使用场景:
实体为父类,子类关系时,数据库的映射有三种方式。
一、joined_subclass:
优缺点:
优点:
1.一个类对应一张表。
缺点:
1.查询子类的数据是连接查询。
2.子类可以添加非空约束。
3.没有冗余的字段。
Persion.java:
package com.shuoeasy.test.joined_subclass; public class Persion { protected int id; protected String name; protected int age; 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Persion [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
Student.java:
package com.shuoeasy.test.joined_subclass; public class Student extends Persion { private String school; public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } @Override public String toString() { return "Student [id=" + super.id + ", name=" + super.name + ", age=" + super.age + ", school=" + school + "]"; } }
Persion.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"> <!-- Generated 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping package="com.shuoeasy.test.subclass"> <class name="Persion" table="PERSION" discriminator-value="TYPE_PERSION"> <id name="id" type="int"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="age" type="int"> <column name="AGE" /> </property> <joined-subclass name="Student" table="STUDENTS"> <key column="STUDENT_ID"></key> <property name="school" type="java.lang.String" column="SCHOOL"></property> </joined-subclass> </class> </hibernate-mapping>
AppTest.java:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping package="com.shuoeasy.test.joined_subclass"> <class name="Persion" table="PERSION" discriminator-value="TYPE_PERSION"> <id name="id" type="int"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="age" type="int"> <column name="AGE" /> </property> <joined-subclass name="Student" table="STUDENTS"> <key column="STUDENT_ID"></key> <property name="school" type="java.lang.String" column="SCHOOL"></property> </joined-subclass> </class> </hibernate-mapping>
表数据
persion:
students:
二、subclass:
优缺点:
优点:
1.一张表解决父类子类对象的数据。
缺点:
1.使用了辨别者列。
2.子类独有的字段不能添加非空约束。
3.若继承层次深,数据库的表字段也会变多。
Persion.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"> <!-- Generated 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping package="com.shuoeasy.test.subclass"> <class name="Persion" table="PERSION" discriminator-value="TYPE_PERSION"> <id name="id" type="int"> <column name="ID" /> <generator class="native" /> </id> <!-- 配置辨别者列 --> <discriminator column="TYPE" type="java.lang.String"></discriminator> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="age" type="int"> <column name="AGE" /> </property> <subclass name="Student" discriminator-value="TYPE_STUDENT"> <property name="school" type="java.lang.String" column="SCHOOL"></property> </subclass> </class> </hibernate-mapping>
表数据
persion:
三、union_subclass:
优缺点:
优点:
1.查询子类的数据效率快。
缺点:
1.不能使用数据库自增方式的主键。
2.查询父类时父表和子表汇总到一起在查询,性能稍慢。
3.查询子类记录只需要一张表数据。
Persion.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"> <!-- Generated 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping package="com.shuoeasy.test.union_subclass"> <class name="Persion" table="PERSION" discriminator-value="TYPE_PERSION"> <id name="id" type="int"> <column name="ID" /> <generator class="hilo" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="age" type="int"> <column name="AGE" /> </property> <union-subclass name="Student" table="STUDENT"> <property name="school" column="SCHOOL"></property> </union-subclass> </class> </hibernate-mapping>
表数据:
hibernate_unique_key:
persion:
student: