java学习

java学习

 

hibernate中实体类继承,按照子类建表

实体类父类:

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;
 }
}

子类:

public class Bird extends Animal {

 private int height;

 public int getHeight() {
  return height;
 }

 public void setHeight(int height) {
  this.height = height;
 }
}


public class Pig extends Animal {
 
 private int weight;

 public int getWeight() {
  return weight;
 }

 public void setWeight(int weight) {
  this.weight = weight;
 }
}


extends.hbm.xml文件:
<hibernate-mapping package="com.hibernate">
 <class name="Animal" abstract="true">
  <id name="id">
   <generator class="assigned"/>
  </id>
  <property name="name"/>
  <property name="sex"/>
  <union-subclass name="Pig" table="t_pig">
   <property name="weight"/>
  </union-subclass>
  <union-subclass name="Bird" table="t_bird">
   <property name="height"/>
  </union-subclass>
 </class>
</hibernate-mapping>
数据库表如下:

posted on 2013-05-07 09:46 杨军威 阅读(298) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜