类表继承
时间:2011-02-19 来源:魔战
package com.jzq.hibernate;
public class Animal {
private int id;
private String name;
private String sex;
public Animal(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return n
}
}
bird类:
package com.jzq.hibernate;
public class Bird extends Animal {
private int height;
public Bird(){}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
Pig类:
package com.jzq.hibernate;
public class Pig extends Animal {
private int height;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Pig(){}
}
配置文件:
<!-- 每个具体类映射成一张表,其中Animal表位抽象空表 -->
<hibernate-mapping>
<class name="Animal" package="com.jzq.hibernate">
<id name="id">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="sex"/>
<!--
连接子类 Pig
表中的字段:id,name,sex,weight
-->
<union-subclass name="Pig" table="t_pig">
<property name="weight"/>
</union-subclass>
<!--
连接子类 Bird
表中的字段:id,name,sex,height
-->
<union-subclass name="Bird" table="t_bird">
<property name="height"/>
</union-subclass>
</class>
</hibernate-mapping>
在代码程序中要手动分配键值