在域模型中,类与类之间除了关联关系和聚集关系,还可以存在继承关系,在下图所示的域模型中,Deparment类和Employee类之间为一对多的双向关联关系,Employee类有两个子类:Skiller类和Sales类。由于Java只允许一个类最多有一个直接的父类,因此Employee类、 Skiller类和Sales类构成了一棵继承关系树。
在面向对象的范畴中,还存在多态的概念,多态建立在继承关系的基础上。简单地理解,多态是指当一个Java应用变量被声明为Employee类时,这个变量实际上既可以引用Employee类自己的实例,Skiller类的实例,也可以引用Sales类的实例。Department类的getEmps()方法通过Hibernate API从数据库中检索出所有Employee对象。getEmps()方法返回的集合既可以包含Employee类自己的实例,Skiller类的实例,也可以引用Sales类的实例。,这种查询被称为多态查询。数据库表之间并不存在继承关系,那么如何把域模型的继承关系映射到关系数据模型中呢?hibernate有以下三种映射方式:
继承关系树的根类对应一个表:对关系数据模型进行非常规设计,在数据库表中加入额外的区分子类型的字段。通过这种方式,可以使关系数据模型支持继承关系和多态。
继承关系树的每个类对应一个表(子类与父类通过外键关联):在关系数据模型中用外键参照关系来表示继承关系。
继承关系树的每个具体类对应一个表:关系数据模型完全不支持域模型中的继承关系和多态。
1.继承关系树的根类对应一个表employee(整个继承树一张表):
employee的表结构如下所示:
mysql> desc employee;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| type | int(11) | NO | | NULL | |
| name | varchar(255) | YES | UNI | NULL | |
| depart_id | int(11) | YES | MUL | NULL | |
| skill | varchar(255) | YES | | NULL | |
| saleAmount | int(11) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
实体类Department和Employee请参看我前面的文章,Skiller和Sales分别如下所示:
- package com.reiyen.hibernate.domain;
- public class Skiller extends Employee {
- private String skill;
- //setter和getter方法
- }
package com.reiyen.hibernate.domain; public class Skiller extends Employee { private String skill; //setter和getter方法 }
- package com.reiyen.hibernate.domain;
- public class Sales extends Employee {
- private int saleAmount;
- //setter和getter方法
- }
package com.reiyen.hibernate.domain; public class Sales extends Employee { private int saleAmount; //setter和getter方法 }
Employee.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.reiyen.hibernate.domain">
- <class name="Employee" discriminator-value="0">
- <id name="id">
- <generator class="native" />
- </id>
- <!--discriminator(鉴别器):缺省类型为string,这里指定为int类型 -->
- <discriminator column="type" type="int"></discriminator>
- <property name="name" unique="true"/>
- <!-- name="department" 这个名称必须与Employee中的属性名一致. 设置了column="depart_id",默认它会去department中找id与depart_id值相等的对象.如果要找name的值与depart_id相等的对象,则可以设置property-ref="name" -->
- <many-to-one name="department" column="depart_id" />
- <subclass name="Skiller" discriminator-value="1">
- <property name="skill" />
- </subclass>
- <subclass name="Sales" discriminator-value="2">
- <property name="saleAmount" />
- </subclass>
- </class>
- </hibernate-mapping>
<?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.reiyen.hibernate.domain"> <class name="Employee" discriminator-value="0"> <id name="id"> <generator class="native" /> </id> <!--discriminator(鉴别器):缺省类型为string,这里指定为int类型 --> <discriminator column="type" type="int"></discriminator> <property name="name" unique="true"/> <!-- name="department" 这个名称必须与Employee中的属性名一致. 设置了column="depart_id",默认它会去department中找id与depart_id值相等的对象.如果要找name的值与depart_id相等的对象,则可以设置property-ref="name" --> <many-to-one name="department" column="depart_id" /> <subclass name="Skiller" discriminator-value="1"> <property name="skill" /> </subclass> <subclass name="Sales" discriminator-value="2"> <property name="saleAmount" /> </subclass> </class> </hibernate-mapping>
测试类如下:
- public class Many2One {
- public static void main(String[] args) {
- add();
- query(1);//1
- }
- static void query(int empId) {
- Session s = null;
- Transaction tx = null;
- try {
- s = HibernateUtil.getSession();
- tx = s.beginTransaction();
- Employee emp = (Employee) s.get(Employee.class, empId);//2
- System.out.println(emp.getClass());
- tx.commit();
- } finally {
- if (s != null)
- s.close();
- }
- }
- static void add() {
- Session s = null;
- Transaction tx = null;
- try {
- Department depart = new Department();
- depart.setName("department name");
- Employee employee1 = new Employee();
- employee1.setDepartment(depart); //1 对象模型:建立两个对象的关联
- employee1.setName("employee1 name1");
- Skiller employee2 = new Skiller();
- employee2.setDepartment(depart); //2 对象模型:建立两个对象的关联
- employee2.setName("employee2 name2");
- employee2.setSkill("j2se");
- Sales employee3 = new Sales();
- employee3.setDepartment(depart); //2 对象模型:建立两个对象的关联
- employee3.setName("employee3 name3");
- employee3.setSaleAmount(1000);
- s = HibernateUtil.getSession();
- tx = s.beginTransaction();
- s.save(depart);
- s.save(employee1);
- s.save(employee2);
- s.save(employee3);
- tx.commit();
- } finally {
- if (s != null)
- s.close();
- }
- }
- }
public class Many2One { public static void main(String[] args) { add(); query(1);//1 } static void query(int empId) { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); Employee emp = (Employee) s.get(Employee.class, empId);//2 System.out.println(emp.getClass()); tx.commit(); } finally { if (s != null) s.close(); } } static void add() { Session s = null; Transaction tx = null; try { Department depart = new Department(); depart.setName("department name"); Employee employee1 = new Employee(); employee1.setDepartment(depart); //1 对象模型:建立两个对象的关联 employee1.setName("employee1 name1"); Skiller employee2 = new Skiller(); employee2.setDepartment(depart); //2 对象模型:建立两个对象的关联 employee2.setName("employee2 name2"); employee2.setSkill("j2se"); Sales employee3 = new Sales(); employee3.setDepartment(depart); //2 对象模型:建立两个对象的关联 employee3.setName("employee3 name3"); employee3.setSaleAmount(1000); s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.save(depart); s.save(employee1); s.save(employee2); s.save(employee3); tx.commit(); } finally { if (s != null) s.close(); } } }
程序运行后,控制台打印信息如下所示:
Hibernate: insert into Department (name) values (?)
Hibernate: insert into Employee (name, depart_id, type) values (?, ?, 0)
Hibernate: insert into Employee (name, depart_id, skill, type) values (?, ?, ?, 1)
Hibernate: insert into Employee (name, depart_id, saleAmount, type) values (?, ?, ?, 2)
Hibernate: select employee0_.id as id1_0_, employee0_.name as name1_0_, employee0_.depart_id as depart4_1_0_, employee0_.skill as skill1_0_, employee0_.saleAmount as saleAmount1_0_, employee0_.type as type1_0_ from Employee employee0_ where employee0_.id=?
class com.reiyen.hibernate.domain.Employee
employee表中记录如下所示:
mysql> select * from employee;
+----+------+-----------------+-----------+-------+------------+
| id | type | name | depart_id | skill | saleAmount |
+----+------+-----------------+-----------+-------+------------+
| 1 | 0 | employee1 name1 | 1 | NULL | NULL |
| 2 | 1 | employee2 name2 | 1 | j2se | NULL |
| 3 | 2 | employee3 name3 | 1 | NULL | 1000 |
+----+------+-----------------+-----------+-------+------------+
3 rows in set (0.00 sec)
将测试代码中注释为1的语句改成:
- query(2);
query(2);
再运行,控制台打印的class如下所示(因为hibernate支持多态查询): class com.reiyen.hibernate.domain.Skiller
打印的查询语句还是如上面所示的没有改变。
在上面修改的基础上,再将测试代码中注释为2的语句改成:
- Employee emp = (Employee) s.get(Skiller.class, empId);
Employee emp = (Employee) s.get(Skiller.class, empId);
再运行,则控制台打印的查询语句为:
Hibernate: select skiller0_.id as id1_0_, skiller0_.name as name1_0_, skiller0_.depart_id as depart4_1_0_, skiller0_.skill as skill1_0_ from Employee skiller0_ where skiller0_.id=? and skiller0_.type=1
class com.reiyen.hibernate.domain.Skiller
优点:操作效率高
缺点:如果说给employee增加子类的话,必须修改表结构,给表结构增加一个字段;同时表中对应子类的字段不能有非空约束.
2.继承关系树的每子类对应一个表(joined-subclass),表结构如下所示:
修改Employee.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.reiyen.hibernate.domain">
- <class name="Employee">
- <id name="id">
- <generator class="native" />
- </id>
- <property name="name" unique="true"/>
- <!-- name="department" 这个名称必须与Employee中的属性名一致. 设置了column="depart_id",默认它会去department中找id与depart_id值相等的对象.如果要找name的值与depart_id相等的对象,则可以设置property-ref="name" -->
- <many-to-one name="department" column="depart_id" />
- <joined-subclass name="Skiller" table="skiller">
- <key column="employee_id" />
- <property name="skill" />
- </joined-subclass>
- <joined-subclass name="Sales" table="sales">
- <key column="employee_id" />
- <property name="saleAmount" column="sale_amount" />
- </joined-subclass>
- </class>
- </hibernate-mapping>
<?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.reiyen.hibernate.domain"> <class name="Employee"> <id name="id"> <generator class="native" /> </id> <property name="name" unique="true"/> <!-- name="department" 这个名称必须与Employee中的属性名一致. 设置了column="depart_id",默认它会去department中找id与depart_id值相等的对象.如果要找name的值与depart_id相等的对象,则可以设置property-ref="name" --> <many-to-one name="department" column="depart_id" /> <joined-subclass name="Skiller" table="skiller"> <key column="employee_id" /> <property name="skill" /> </joined-subclass> <joined-subclass name="Sales" table="sales"> <key column="employee_id" /> <property name="saleAmount" column="sale_amount" /> </joined-subclass> </class> </hibernate-mapping>
测试类不变,只是将测试代码中注释为1的语句改成:
- query(2);
query(2);
则控制台打印的信息如下所示:
Hibernate: insert into Department (name) values (?)
Hibernate: insert into Employee (name, depart_id) values (?, ?)
Hibernate: insert into Employee (name, depart_id) values (?, ?)
Hibernate: insert into skiller (skill, employee_id) values (?, ?)
Hibernate: insert into Employee (name, depart_id) values (?, ?)
Hibernate: insert into sales (sale_amount, employee_id) values (?, ?)
Hibernate: select employee0_.id as id1_0_, employee0_.name as name1_0_, employee0_.depart_id as depart3_1_0_, employee0_1_.skill as skill2_0_, employee0_2_.sale_amount as sale2_3_0_, case when employee0_1_.employee_id is not null then 1 when employee0_2_.employee_id is not null then 2 when employee0_.id is not null then 0 end as clazz_0_ from Employee employee0_ left outer join skiller employee0_1_ on employee0_.id=employee0_1_.employee_id left outer join sales employee0_2_ on employee0_.id=employee0_2_.employee_id where employee0_.id=?
class com.reiyen.hibernate.domain.Skiller
从打印的SQL语句可以看出,此时,如果保存的是Employee对象的子类的实例的话,则要在两张表中保存记录;如果查询的是子类对象的话,是三张表关联在一起进行查询。
在上面修改的基础上,再将测试代码中注释为2的语句改成:
- Employee emp = (Employee) s.get(Skiller.class, empId);
Employee emp = (Employee) s.get(Skiller.class, empId);
再运行,则控制台打印的查询语句为:
Hibernate: select skiller0_.employee_id as id1_0_, skiller0_1_.name as name1_0_, skiller0_1_.depart_id as depart3_1_0_, skiller0_.skill as skill2_0_ from skiller skiller0_ inner join Employee skiller0_1_ on skiller0_.employee_id=skiller0_1_.id where skiller0_.employee_id=?
此时只关联两张表查询。
数据库中表记录如下所示:
mysql> select * from employee;
+----+-----------------+-----------+
| id | name | depart_id |
+----+-----------------+-----------+
| 1 | employee1 name1 | 1 |
| 2 | employee2 name2 | 1 |
| 3 | employee3 name3 | 1 |
+----+-----------------+-----------+
3 rows in set (0.00 sec)
mysql> select * from skiller;
+-------------+-------+
| employee_id | skill |
+-------------+-------+
| 2 | j2se |
+-------------+-------+
1 row in set (0.00 sec)
mysql> select * from sales;
+-------------+-------------+
| employee_id | sale_amount |
+-------------+-------------+
| 3 | 1000 |
+-------------+-------------+
1 row in set (0.00 sec)
3.混合使用,假设如果Sales的属性很多,而Skiller的属性很少,这时可以混使用“一个类继承体系一张表”和“每个子类一张表”,表结构如下所示:
Employee.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.reiyen.hibernate.domain">
- <class name="Employee" discriminator-value="0">
- <id name="id">
- <generator class="native" />
- </id>
- <discriminator column="type" type="int" />
- <property name="name" unique="true" />
- <!-- name="department" 这个名称必须与Employee中的属性名一致. 设置了column="depart_id",默认它会去department中找id与depart_id值相等的对象.如果要找name的值与depart_id相等的对象,则可以设置property-ref="name" -->
- <many-to-one name="department" column="depart_id" />
- <!--如果discriminator-value没有显式的给定值的话,则与name属性的值保持一致,即为Skiller -->
- <subclass name="Skiller" discriminator-value="1">
- <property name="skill" />
- </subclass>
- <subclass name="Sales" discriminator-value="2">
- <join table="sales">
- <key column="employee_id" />
- <property name="saleAmount" column="sale_amount" />
- </join>
- </subclass>
- </class>
- </hibernate-mapping>
<?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.reiyen.hibernate.domain"> <class name="Employee" discriminator-value="0"> <id name="id"> <generator class="native" /> </id> <discriminator column="type" type="int" /> <property name="name" unique="true" /> <!-- name="department" 这个名称必须与Employee中的属性名一致. 设置了column="depart_id",默认它会去department中找id与depart_id值相等的对象.如果要找name的值与depart_id相等的对象,则可以设置property-ref="name" --> <many-to-one name="department" column="depart_id" /> <!--如果discriminator-value没有显式的给定值的话,则与name属性的值保持一致,即为Skiller --> <subclass name="Skiller" discriminator-value="1"> <property name="skill" /> </subclass> <subclass name="Sales" discriminator-value="2"> <join table="sales"> <key column="employee_id" /> <property name="saleAmount" column="sale_amount" /> </join> </subclass> </class> </hibernate-mapping>
此时测试类不变,只是将测试代码中注释为1的语句改成:
- query(2);
query(2);
然后在上面的基础上运行原程序,则控制台会打印出如下异常信息:
Hibernate: insert into Department (name) values (?)
Hibernate: insert into Employee (name, depart_id, type) values (?, ?, 0)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.reiyen.hibernate.domain.Employee]
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'type' in 'field list'
这是因为我在hibernate.cfg.xml配置文件中配置了此项:
- <property name="hbm2ddl.auto">create</property>
<property name="hbm2ddl.auto">create</property>
所以在此次程序运行时,会删除数据库中的employee表,sales表,而employee表中有skiller表的外键关联,所以不能删除employee数据表了,所以抛出了上面的异常。此时你再查看数据库表,如下所示 :
mysql> select * from employee;
+----+-----------------+-----------+
| id | name | depart_id |
+----+-----------------+-----------+
| 1 | employee1 name1 | 1 |
| 2 | employee2 name2 | 1 |
| 3 | employee3 name3 | 1 |
+----+-----------------+-----------+
3 rows in set (0.00 sec)
mysql> select * from skiller;
+-------------+-------+
| employee_id | skill |
+-------------+-------+
| 2 | j2se |
+-------------+-------+
1 row in set (0.00 sec)
mysql> select * from sales;
Empty set (0.00 sec)
所以得先手动删除skiller数据表,然后再来运行程序:
如果Employee.hbm.xml配置文件中
- <subclass name="Skiller" >
<subclass name="Skiller" >
不配置discriminator-value="1",则会抛出如下异常:
java.lang.ExceptionInInitializerError
Caused by: org.hibernate.MappingException: Could not format discriminator value to SQL string
因为如果discriminator-value没有显式的给定值的话,则与name属性的值保持一致,即为Skiller ,所以会抛出如上异常!
4.继承关系树的每个具体类对应一个表(union-subclass)
表结构如下所示:
Employee.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.reiyen.hibernate.domain">
- <class name="Employee">
- <id name="id">
- <generator class="hilo" />
- </id>
- <property name="name" unique="true" />
- <many-to-one name="department" column="depart_id" />
- <union-subclass name="Skiller" table="skiller">
- <property name="skill" />
- </union-subclass>
- <union-subclass name="Sales" table="sales">
- <property name="saleAmount" column="sale_amount" />
- </union-subclass>
- </class>
- </hibernate-mapping>
<?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.reiyen.hibernate.domain"> <class name="Employee"> <id name="id"> <generator class="hilo" /> </id> <property name="name" unique="true" /> <many-to-one name="department" column="depart_id" /> <union-subclass name="Skiller" table="skiller"> <property name="skill" /> </union-subclass> <union-subclass name="Sales" table="sales"> <property name="saleAmount" column="sale_amount" /> </union-subclass> </class> </hibernate-mapping>
此时主键增长不能再是:
- <generator class="native" />
<generator class="native" />
因为如果使用native的话三张表会产生相同的id值,这样当根据id查询Employee时就会出错了。所以如果你配置成native时会抛出如下异常(因为Employee实体类中id对应 的是int了,所以在此使用hilo(高低位)主键生成方式):
org.hibernate.MappingException: Cannot use identity column key generation with <union-subclass> mapping for: com.reiyen.hibernate.domain.Skiller
运行测试程序后,此时控制台打印信息如下所示:
Hibernate: insert into Department (name) values (?)
Hibernate: insert into Employee (name, depart_id, id) values (?, ?, ?)
Hibernate: insert into skiller (name, depart_id, skill, id) values (?, ?, ?, ?)
Hibernate: insert into sales (name, depart_id, sale_amount, id) values (?, ?, ?, ?)
Hibernate: select employee0_.id as id1_0_, employee0_.name as name1_0_, employee0_.depart_id as depart3_1_0_, employee0_.skill as skill2_0_, employee0_.sale_amount as sale1_3_0_, employee0_.clazz_ as clazz_0_ from ( select id, null as sale_amount, depart_id, null as skill, name, 0 as clazz_ from Employee union select id, null as sale_amount, depart_id, skill, name, 1 as clazz_ from skiller union select id, sale_amount, depart_id, null as skill, name, 2 as clazz_ from sales ) employee0_ where employee0_.id=?
class com.reiyen.hibernate.domain.Skiller
执行查询时,首先使用子查询,在子查询中使用union将三张表的结果全成一张表,然后再在合成的结果集中进行查询。
如果Employee是一个抽象类, 你不想 在数据表中对应相应的数据表,则可以设置abstract="true" .如下所示:
- <class name="Employee" abstract="true" >
<class name="Employee" abstract="true" >
此外,如果继承关系中有接口,可以把它当作抽象类对待。
三种映射方式的比较和选择
为了方便说明为三种方式按顺序标号为[1]整个继承树一张表;[2]每子类对应一个表(joined-subclass);[4]每个具体类对应一个表(union-subclass)。
1、复杂度:
[1]简单;
[2]表较多且之间有外键约束;
[4]包含重复字段;
2、查询性能:
[1]效率高;
[2]需要表内连接或左外连接;
[4]若查询父类需查所有子类表;
3、可维护性:
[1]只需修改一个表;
[2]若某个类属性变化只修改这个类对应的表;
[4]若父类属性变化需要修改所有子类对应的表;
综上,选择时,可以参考以下原则:
1、子类属性不是非常多时,优先考虑[1],因为其性能最佳。
2、子类属性非常多,且对性能要求不是很严格时,优先考虑[2]
相关推荐
本主题将深入探讨"Hibernate继承映射+C3P0代码"的相关知识点。 首先,让我们理解Hibernate的继承映射。在Java中,我们可以创建一个基类,然后派生出多个子类,这种设计模式在数据库中也可以被映射出来。Hibernate...
Hibernate继承映射是将Java类的继承关系映射到数据库表的一种策略,使得对象模型的复杂性能够平滑地转化为关系数据库模型。本篇将详细介绍Hibernate继承映射的第一种策略——每棵类继承树对应一张表,即单一表继承...
《Hibernate继承映射详解》 Hibernate,作为Java领域中的一款著名对象关系映射(ORM)框架,极大地简化了数据库操作。在实际项目中,我们常常会遇到类的继承关系,而Hibernate提供了强大的支持来处理这种继承关系的...
**标题:“Hibernate继承映射(Annotation)详解”** 在Java持久化框架Hibernate中,继承映射是一种关键特性,它允许开发者将对象模型的继承结构映射到数据库的表结构。在传统的面向对象编程中,继承是实现代码复用和...
《Hibernate继承映射详解》 在Java开发中,对象关系映射(ORM)框架如Hibernate大大简化了数据库操作。Hibernate不仅提供了对基本数据类型的映射,还支持复杂的数据结构,如继承关系的映射。本篇文章将深入探讨...
本篇文章将详细探讨Hibernate继承映射的策略,特别是“每个具体类一张表”(Table Per Concrete Class)的映射方式。 在面向对象编程中,继承是常见的代码复用手段,但在关系型数据库中,这种概念并不直接对应。...
本文将详细探讨“Hibernate继承映射的第一种策略:每个类对应一张表”的概念、实现方式以及其优缺点。 首先,我们需要理解Hibernate继承映射的基本策略。在面向对象编程中,类继承是常见的代码复用手段,但在数据库...
【Hibernate继承映射】是Java开发中使用Hibernate框架进行数据持久化时的一种重要技术,它允许我们将复杂的对象模型映射到数据库的表结构上。在实际项目中,我们经常遇到对象之间的继承关系,如抽象类和子类。...
针对hibernate的继承映射部分做了几个测试的例子,更加直观的阐述了继承映射的几种方式以及对应的表结构方式,文件被分割成3部分 http://download.csdn.net/source/259075 http://download.csdn.net/source/259072 ...
本篇主要探讨的是Hibernate中的继承映射策略,特别是“每个子类一张表”(Table per subclass)的方式。这种方式也被称为单表继承或多表继承,是Hibernate支持的四种继承映射策略之一。了解并熟练掌握这种映射模式...
### 用Hibernate映射继承关系 #### 14.1 继承关系树的每个具体类对应一个表 在Hibernate中映射继承关系时,一种常见的策略是将继承关系树的每个具体类映射到单独的数据库表中。这种方法称为**表/类映射**(Table/...
本篇文章主要探讨的是Hibernate的继承映射策略,特别是“每个类分层结构一张表”(Table per Concrete Class)的方式。这种映射策略是Hibernate提供的多种继承映射方案之一,适用于处理复杂的对象模型。 首先,我们...
在Java世界中,ORM(对象关系映射)框架如Hibernate极大地简化了数据库操作。本教程主要探讨的是Hibernate中的“继承映射...学习并实践这些例子,有助于巩固对Hibernate继承映射的理解,并提升在实际项目中的应用能力。