一、hibernate继承映射之union方式
union方式:子类从父类继承的字段和自己独有的字段都存放在自己的表里。
二、Student继承Person为例
三、代码
public class Person {
private Integer id;
private String name;
private String age;
//get/set...
}
public class Student extends Person {
private String school;
//get/set...
}
Person.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 2017-3-27 22:49:29 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="com.hibernate.n2n">
<class name="Person" table="PERSONS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="hilo" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="java.lang.String">
<column name="AGE" />
</property>
<union-subclass name="Student" table="STUDENTS">
<property name="school" column="SCHOOL" type="string"></property>
</union-subclass>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置链接数据库的基本信息 -->
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate01</property>
<!-- hibernate的基本信息 -->
<!-- hibernate使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 执行操作时是否在控制台打印sql语句 -->
<property name="show_sql">true</property>
<!-- 是否对sql进行格式化 -->
<property name="format_sql">true</property>
<!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 设置hibernate的隔离级别 -->
<property name="connection.isolation">2</property>
<!-- 删除对象后,设置其OID为null -->
<property name="use_identifier_rollback">true</property>
<!-- 配置C3P0数据源 -->
<property name="c3p0.max_size">100</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.acquire_increment">2</property>
<property name="c3p0.idle_test_period">2000</property>
<property name="c3p0.timeout">2000</property>
<property name="c3p0.max_statements">10</property>
<!-- 设置JDBC的statement读取数据库的时候每次从数据库中读取记录条数,这个值100合适,太大会损耗内存,小了速度慢 -->
<property name="jdbc.fetch_size">100</property>
<!-- 设置数据库进行批量删除,更新,插入的时候批次的大小,30比较合适 -->
<property name="jdbc.batch_size">30</property>
<!-- 指定关联的hbm.xml文件 -->
<mapping resource="com/hibernate/n2n/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
四、测试Test
package com.hibernate.n2n;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HibernateTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Test
public void testSave(){
Person person = new Person();
person.setAge("22");
person.setName("AA");
session.save(person);
Student student = new Student();
student.setSchool("BB");
student.setAge("18");
student.setName("CC");
session.save(student);
}
@Test
public void testQuery(){
//查询父类数据,只需查询一张表
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());
}
/***
* 更新父表,性能差,效率低
*/
@Test
public void testUpdate(){
String hql = "UPDATE Person p SET p.age = 20";
session.createQuery(hql).executeUpdate();
}
@Before
public void init() {
System.out.println("HibernateTest.init()");
// 1.创建一个sessionFactory对象
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
// 2.创建一个session对象
session = sessionFactory.openSession();
// 3.开启事务
transaction = session.beginTransaction();
}
@After
public void destroy() {
System.out.println("HibernateTest.destroy()");
// 5.提交事务
transaction.commit();
// 6.关闭session
session.close();
// 7.关闭SessionFactory
sessionFactory.close();
}
}
五、运行结果
hibernate_unique_key是主键的生成方式为Hilo时,自动生成的表,目的是为了保证表主键的唯一性
Person表
Student表