Hibernate 继承类 每棵树 具体的独立的表

来源:互联网 发布:matlab字符分割算法 编辑:程序博客网 时间:2024/06/16 10:20

Animal类

public class Animal {
private int id;
private String name;
private boolean sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
} 

------------------------------------------------------------

Pig类

public class Pig extends Animal {
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}

---------------------------------------------------------------

Bird类

public class Bird extends Animal {

private int height;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}----------------------------------------------------------

映射文件

Animal.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.soft.gread1">
<class name="Animal" table="t_animal" lazy="false" >
<id name="id" >
            <!-- 此时主键生成策略  不能使用native identity 因为每个子类id不能相同  或者设置为手动-->
            <generator class="increment"/>        
</id>
<property name="name"/>
<property name="sex"/>

<union-subclass name="Pig" table="t_pig">
<property name="weight"></property>
</union-subclass>

<union-subclass name="Bird" table="t_bird">
<property name="height"></property>
</union-subclass>

</class>
</hibernate-mapping>