映射繼承關(guān)系有三種方法
- 使用subclass元素子類數(shù)據(jù)和父類數(shù)據(jù)都存在一張表中 通過鑒別者列值進(jìn)行區(qū)分
1.查詢父類記錄缓窜,只需要查詢一張數(shù)據(jù)表
2.對于子記錄橡伞,也只需查詢一張表
缺點(diǎn):
1.使用辨別者列
2.子類獨(dú)有字段無法添加非空判斷
3.若繼承較深炼幔,數(shù)據(jù)表字段也會較多
- oined-subclass元素 子類會生成一張新表 保存父類沒有的列參照父類OID生成子類OID
1.查詢父類記錄,做一個左外鏈接查詢
2.對于子類查詢践宴,做一個內(nèi)鏈接查詢
優(yōu)點(diǎn):
1.不需要使用辨別者列
2.子類獨(dú)有字段可以加非空約束
3.沒有冗余字段
- union-subclass元素 子類表和父類包都是完整的表
1.查詢父類記錄哨查,需要把父表和子表做匯總查詢
2.對于子類查詢,只需要查一張表
缺點(diǎn):
1.存在冗余字段
2.父類更新麻煩效率低
父類
public class Person {
private Integer id;
private String name;
private int age;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
子類
public class Student extends Person {
private String school;
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
subclass元素
映射文件
只需要設(shè)置父類
配置鑒別者列 <discriminator column="TYPE" type="string"></discriminator>
discriminator-value指定辨別者類的值
映射子類 subclass映射
<hibernate-mapping>
<class name="chen.Person" table="PERSON" discriminator-value="PERSON">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<!-- 配置鑒別者列 -->
<!-- discriminator-value指定辨別者類的值 -->
<discriminator column="TYPE" type="string"></discriminator>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<!-- 映射子類 subclass映射 -->
<subclass name="chen.Student" discriminator-value="STUDENT">
<property name="school" type="string" column="SCHOOL"></property>
</subclass>
</class>
</hibernate-mapping>
subclass元素 保存
/**
* 映射繼承關(guān)系 subclass元素在一張表中
*/
public static void testExtendsSubclass() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person=new Person();
person.setAge(20);
person.setName("張三");
Student student=new Student();
student.setAge(24);
student.setName("王五");
student.setSchool("呵呵呵");
session.save(person);
session.save(student);
transaction.commit();
session.close();
sessionFactory.close();
}
subclass元素 查詢
/**
* 映射繼承關(guān)系 subclass元素 查詢
* 1.查詢父類記錄弦疮,只需要查詢一張數(shù)據(jù)表
* 2.對于子記錄夹攒,也只需查詢一張表
* 缺點(diǎn):
* 1.使用辨別者列
* 2.子類獨(dú)有字段無法添加非空判斷
* 3.若繼承較深,數(shù)據(jù)表字段也會較多
* */
public static void testExtendsSubclassQuery() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
//createQuery參數(shù)是類名
List<Person> persons = session.createQuery("FROM Person").list();
System.out.println(persons.size());
List<Student> students = session.createQuery("FROM Student").list();
System.out.println(students.size());
transaction.commit();
session.close();
sessionFactory.close();
}
joined-subclass元素
配置映射文件
<hibernate-mapping>
<class name="cheniso.Person" table="PERSONISO">
<id name="id" type="java.lang.Integer">
<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="cheniso.Student" table="STUDENT">
<!-- 參照父表主鍵生成子表主鍵 -->
<key column="STUDENT_ID"></key>
<property name="school" column="school"></property>
</joined-subclass>
</class>
</hibernate-mapping>
保存
/**
* 映射繼承關(guān)系 joined-subclass元素
* 1.子類會生成一張新表 保存父類沒有的列
* 2.對于子類至少要插入兩張表
*/
public static void testExtendsJoinedSubclass() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
cheniso.Person person=new cheniso.Person();
person.setAge(20);
person.setName("張三");
cheniso.Student student=new cheniso.Student();
student.setAge(24);
student.setName("王五");
student.setSchool("呵呵呵");
session.save(person);
session.save(student);
transaction.commit();
session.close();
sessionFactory.close();
}
查詢
/**
* 映射繼承關(guān)系 joined-subclass元素 查詢
* 1.查詢父類記錄胁塞,做一個左外鏈接查詢
* 2.對于子類查詢咏尝,做一個內(nèi)鏈接查詢
* 優(yōu)點(diǎn):
* 1.不需要使用辨別者列
* 2.子類獨(dú)有字段可以加非空約束
* 3.沒有冗余字段
* */
public static void testExtendsJoinedSubclassQuery() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
List<cheniso.Person> persons = session.createQuery("FROM cheniso.Person").list();
System.out.println(persons.size());
List<cheniso.Student> students = session.createQuery("FROM cheniso.Student").list();
System.out.println(students.size());
transaction.commit();
session.close();
sessionFactory.close();
}
union-subclass元素
配置映射文件
- 主主鍵生成方式increment
<hibernate-mapping>
<class name="chenisoa.Person" table="PERSONISOA">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<union-subclass name="chenisoa.Student" table="studentisoa">
<property name="school" column="SCHOOL" type="string"></property>
</union-subclass>
</class>
</hibernate-mapping>
保存
/**
* 映射繼承關(guān)系 union-subclass元素
* 1.子類表和父類包都是完整的表
* 2.子類插入只需一張表
* 3.子類獨(dú)有字段可以加非空約束
*/
public static void testExtendsUnionSubclass() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
chenisoa.Person person=new chenisoa.Person();
person.setAge(20);
person.setName("張三");
chenisoa.Student student=new chenisoa.Student();
student.setAge(24);
student.setName("王五");
student.setSchool("呵呵呵");
session.save(person);
session.save(student);
transaction.commit();
session.close();
sessionFactory.close();
}
查詢
/**
* 映射繼承關(guān)系 union-subclass元素 查詢
*
* 1.查詢父類記錄,需要把父表和子表做匯總查詢
* 2.對于子類查詢啸罢,只需要查一張表
* 缺點(diǎn):
* 1.存在冗余字段
* 2.父類更新麻煩效率低
* */
public static void testExtendsUnionSubclassQuery() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
List<chenisoa.Person> persons = session.createQuery("FROM chenisoa.Person").list();
System.out.println(persons.size());
List<chenisoa.Student> students = session.createQuery("FROM chenisoa.Student").list();
System.out.println(students.size());
transaction.commit();
session.close();
sessionFactory.close();
}