Hibernate的继承映射
当VO的类之间实现了继承关系的时候,可以通过<joined-subclass>来配制子类与基类之间的继承映射。
应像对应的表之间也存在同样的继承关系。
1.建表
create table animal
(aid varchar(32) not null primary key)
create table dog
(aid varchar(32) not null primary key,
dname varchar(16)
)
create table cat
(aid varchar(32) not null primary key,
cname varchar(16)
)
(aid varchar(32) not null primary key)
create table dog
(aid varchar(32) not null primary key,
dname varchar(16)
)
create table cat
(aid varchar(32) not null primary key,
cname varchar(16)
)
2 写vo
2.1 Animal.java
package com.test;
public class Animal
{
private String aid;
//getter setter
public class Animal
{
private String aid;
//getter setter
2.2
Car.java
package com.test;
public class Cat extends Animal
{
private String cname;
//写上 get set 方法
public class Cat extends Animal
{
private String cname;
//写上 get set 方法
2.3
Dog.java
package com.test;
public class Dog extends Animal
{
private String dname;
//写getter setter
public class Dog extends Animal
{
private String dname;
//写getter setter
3.写配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.test.Animal" table="animal">
<id name="aid" type="string" unsaved-value="null" >
<column name="aid" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<joined-subclass name="com.test.Dog" table="dog">
<key column="aid"/>
<property name="dname">
<column name="dname" sql-type="varchar(16)"/>
</property>
</joined-subclass>
<joined-subclass name="com.test.Cat" table="cat">
<key column="aid"/>
<property name="cname">
<column name="cname" sql-type="varchar(16)"/>
</property>
</joined-subclass>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.test.Animal" table="animal">
<id name="aid" type="string" unsaved-value="null" >
<column name="aid" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<joined-subclass name="com.test.Dog" table="dog">
<key column="aid"/>
<property name="dname">
<column name="dname" sql-type="varchar(16)"/>
</property>
</joined-subclass>
<joined-subclass name="com.test.Cat" table="cat">
<key column="aid"/>
<property name="cname">
<column name="cname" sql-type="varchar(16)"/>
</property>
</joined-subclass>
</class>
</hibernate-mapping>