映射继承关系有三种方法
- 使用subclass元素子类数据和父类数据都存在一张表中 通过鉴别者列值进行区分
1.查询父类记录,只需要查询一张数据表
2.对于子记录,也只需查询一张表
缺点:
1.使用辨别者列
2.子类独有字段无法添加非空判断
3.若继承较深,数据表字段也会较多
- oined-subclass元素 子类会生成一张新表 保存父类没有的列参照父类OID生成子类OID
1.查询父类记录,做一个左外链接查询
2.对于子类查询,做一个内链接查询
优点:
1.不需要使用辨别者列
2.子类独有字段可以加非空约束
3.没有冗余字段
- union-subclass元素 子类表和父类包都是完整的表
1.查询父类记录,需要把父表和子表做汇总查询
2.对于子类查询,只需要查一张表
缺点:
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元素
映射文件
只需要设置父类
配置鉴别者列 <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元素 保存
/**
* 映射继承关系 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元素 查询
/**
* 映射继承关系 subclass元素 查询
* 1.查询父类记录,只需要查询一张数据表
* 2.对于子记录,也只需查询一张表
* 缺点:
* 1.使用辨别者列
* 2.子类独有字段无法添加非空判断
* 3.若继承较深,数据表字段也会较多
* */
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参数是类名
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>
保存
/**
* 映射继承关系 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();
}
查询
/**
* 映射继承关系 joined-subclass元素 查询
* 1.查询父类记录,做一个左外链接查询
* 2.对于子类查询,做一个内链接查询
* 优点:
* 1.不需要使用辨别者列
* 2.子类独有字段可以加非空约束
* 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>
保存
/**
* 映射继承关系 union-subclass元素
* 1.子类表和父类包都是完整的表
* 2.子类插入只需一张表
* 3.子类独有字段可以加非空约束
*/
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();
}
查询
/**
* 映射继承关系 union-subclass元素 查询
*
* 1.查询父类记录,需要把父表和子表做汇总查询
* 2.对于子类查询,只需要查一张表
* 缺点:
* 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();
}