你好,游客 登录 注册 搜索
背景:
阅读新闻

Hibernate继承映射详解

[日期:2012-05-22] 来源:Linux社区  作者:wangpeng [字体:大 中 小]

第一种方案:一个子类对应一张表

该方案是使继承体系中每一个子类都对应数据库中的一张表。

示意图如下:

每一个子类对应的数据库表都包含了父类的信息,并且包含了自己独有的属性。每个子类对应一张表,而且这个表的信息是完备的,即包含了所有从父类继承下来的属性映射的字段。这种策略是使用标签来定义子类的。

配置People.hbm.xml文件:(注意:这里都写在了一个People的配置文件里,也可以分别写在三个配置文件,即Student.hbm.xml、Teacher.hbm.xml里,以下同样可以)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  3.   
  4. <hibernate-mapping>  
  5.     <class name="com.suxiaolei.hibernate.pojos.People" abstract="true">  
  6.         <id name="id" type="string">  
  7.             <column name="ID"></column>  
  8.             <generator class="uuid"></generator>  
  9.         </id>  
  10.   
  11.         <property name="name" column="NAME" type="string"></property>  
  12.         <property name="sex" column="SEX" type="string"></property>  
  13.         <property name="age" column="AGE" type="string"></property>  
  14.         <property name="birthday" column="BIRTHDAY" type="timestamp"></property>  
  15.   
  16.         <!--   
  17.         <union-subclass name="com.suxiaolei.hibernate.pojos.Student" table="STUDENT">   
  18.             <property name="cardId" column="CARDID" type="string"></property>   
  19.         </union-subclass>   
  20.         <union-subclass name="com.suxiaolei.hibernate.pojos.Teacher" table="TEACHER">   
  21.             <property name="salary" column="SALARY" type="integer"></property>   
  22.         </union-subclass>   
  23.         -->  
  24.     </class>  
  25.   
  26.         <!-- 可单独写在Student.hbm.xml里 -->  
  27.     <union-subclass name="com.suxiaolei.hibernate.pojos.Student"  
  28.         table="STUDENT" extends="com.suxiaolei.hibernate.pojos.People">  
  29.         <property name="cardId" column="CARDID" type="string"></property>  
  30.     </union-subclass>  
  31.   
  32.         <!-- 可单独写在Teacher.hbm.xml里 -->  
  33.     <union-subclass name="com.suxiaolei.hibernate.pojos.Teacher"  
  34.         table="TEACHER" extends="com.suxiaolei.hibernate.pojos.People">  
  35.         <property name="salary" column="SALARY" type="integer"></property>  
  36.     </union-subclass>  
  37. </hibernate-mapping>  
linux
相关资讯       hibernate 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款