标签:style blog http io color os 使用 sp java
Hibernate的继承映射可以理解为持久化类之间的继承关系。例如:人和学生之间的关系。学生继承了人,可以认为学生是一个特殊的人,如果对人进行查询,学生的实例也将被得到。
Hibernate支持以下三种继承映射策略:
下面分情况具体了解以上三种映射策略。采用Person作为父类,Student作为子类来详细解释以上三种策略实现。其中Perosn(id, name, age), Studnet(school)
采用subclass进行映射:
Person.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2014-11-30 9:34:50 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping package="com.yl.hibernate.subclass"> 6 <class name="Person" table="PERSONS" discriminator-value="PERSON"> 7 <id name="id" type="java.lang.Integer"> 8 <column name="ID" /> 9 <generator class="native" /> 10 </id> 11 12 <!-- 配置辨别者列 --> 13 <discriminator column="TYPE" type="string"></discriminator> 14 15 <property name="name" type="java.lang.String"> 16 <column name="NAME" /> 17 </property> 18 <property name="age" type="int"> 19 <column name="AGE" /> 20 </property> 21 <!-- 映射子类Student, 使用subclass进行映射 --> 22 <subclass name="Student" discriminator-value="STUDENT"> 23 <property name="school" type="string" column="SCHOOL"></property> 24 </subclass> 25 </class> 26 </hibernate-mapping>
采用joined-subclass进行映射
Person.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2014-11-30 9:34:50 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping package="com.yl.hibernate.joined.subclass"> 6 <class name="Person" table="PERSONS"> 7 <id name="id" type="java.lang.Integer"> 8 <column name="ID" /> 9 <generator class="native" /> 10 </id> 11 12 <property name="name" type="java.lang.String"> 13 <column name="NAME" /> 14 </property> 15 <property name="age" type="int"> 16 <column name="AGE" /> 17 </property> 18 <!-- 映射子类Student, 使用joined-subclass进行映射 --> 19 <joined-subclass name="Student" table="STUDENTS"> 20 <key column="STUDENT_ID"></key> 21 <property name="school" type="string" column="SCHOOL"></property> 22 </joined-subclass> 23 24 </class> 25 </hibernate-mapping>
采用union-subclass进行映射
Person.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2014-11-30 9:34:50 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping package="com.yl.hibernate.union.subclass"> 6 <class name="Person" table="PERSONS"> 7 <id name="id" type="java.lang.Integer"> 8 <column name="ID" /> 9 <generator class="hilo" /> 10 </id> 11 12 <property name="name" type="java.lang.String"> 13 <column name="NAME" /> 14 </property> 15 <property name="age" type="int"> 16 <column name="AGE" /> 17 </property> 18 <!-- 映射子类Student, 使用union-subclass进行映射 --> 19 <union-subclass name="Student" table="STUDENTS"> 20 <property name="school" type="string" column="SCHOOL"></property> 21 </union-subclass> 22 23 24 25 </class> 26 </hibernate-mapping>
标签:style blog http io color os 使用 sp java
原文地址:http://www.cnblogs.com/dreamfree/p/4132857.html