类与表的关系:
*************
Employee.java
*************
package blog.hibernate.domain;
public class Employee {
private int id;
private String name;
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;
}
@Override
public String toString() {
return "Employee{" + "id=" + id + ", name=" + name + '}';
}
}
*************
Sale.java
*************
package blog.hibernate.domain;
public class Sale extends Employee {
private int sell;
public int getSell() {
return sell;
}
public void setSell(int sell) {
this.sell = sell;
}
@Override
public String toString() {
return "Sale{" + "id=" + this.getId() + ", name=" + this.getName() + "sell=" + sell + '}';
}
}
*************
Skill.java
*************
package blog.hibernate.domain;
public class Skill extends Employee{
private String skiller;
public String getSkiller() {
return skiller;
}
public void setSkiller(String skiller) {
this.skiller = skiller;
}
@Override
public String toString() {
return "Skill{" + "id=" + this.getId() + ", name=" + this.getName() + "skiller=" + 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="blog.hibernate.domain">
<class name="Employee" table="employees">
<id name="id" column="EMPLOYEE_ID">
<generator class="hilo" />
</id>
<property name="name" column="EMPLOYEE_NAME" />
<union-subclass name="Sale" table="SALE">
<property name="sell" column="SELL"/>
</union-subclass>
<union-subclass name="Skill" table="SKILL">
<property name="skiller" column="SKILLER"/>
</union-subclass>
</class>
</hibernate-mapping>
*******************
HibernateUtil.java
*******************
package blog.hibernate;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public final class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil(){}
static{
Configuration cfg = new Configuration();
sessionFactory = cfg.configure("hibernate.cfg.xml").buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static Session getSession(){
return sessionFactory.openSession();
}
****************
hibernate.cfg.xml
***************
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/unionextend</property><!-- ///表示连接本机的数据库//localhost:3306 -->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="blog/hibernate/domain/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
******************
junit test: JoinExtend.java
*******************
package junit.test;
import java.util.logging.Logger;
import java.util.logging.Level;
import org.hibernate.Transaction;
import blog.hibernate.HibernateUtil;
import blog.hibernate.domain.Employee;
import blog.hibernate.domain.Sale;
import blog.hibernate.domain.Skill;
import org.hibernate.Session;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class JoinExtend {
public JoinExtend() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@Test
public void test(){
add();
query();
}
public void add(){
Employee emp1 = new Employee();
emp1.setName("lisi");
Skill emp2 = new Skill();
emp2.setName("wangwu");
emp2.setSkiller("java");
Sale emp3 = new Sale();
emp3.setName("sunliu");
emp3.setSell(300000);
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
session.save(emp3);
session.save(emp2);
session.save(emp1);
tx.commit();
} catch (Exception e) {
Logger.getLogger(JoinExtend.class.getName()).log(Level.SEVERE, null, e);
if (tx != null) {
tx.rollback();
}
} finally {
if (session != null) {
session.close();
}
}
}
public void query(){
Session session = null;
try {
session = HibernateUtil.getSession();
Employee emp1 = (Employee)session.get(Sale.class, 1);
Employee emp2 = (Employee)session.get(Skill.class, 2);
Employee emp3 = (Employee)session.get(Employee.class, 3);
System.out.println(emp1.toString());
System.out.println(emp2.toString());
System.out.println(emp3.toString());
} catch (Exception e) {
Logger.getLogger(JoinExtend.class.getName()).log(Level.SEVERE, null, e);
} finally {
if (session != null) {
session.close();
}
}
}
}
生成的表结构:
hibernate_unique_key
employees
sale
skill
插入语句:
Hibernate: insert into SALE (EMPLOYEE_NAME, SELL, EMPLOYEE_ID) values (?, ?, ?)
Hibernate: insert into SKILL (EMPLOYEE_NAME, SKILLER, EMPLOYEE_ID) values (?, ?, ?)
Hibernate: insert into employees (EMPLOYEE_NAME, EMPLOYEE_ID) values (?, ?)
查询语句:
Hibernate:
select sale0_.EMPLOYEE_ID as EMPLOYEE1_0_0_,
sale0_.EMPLOYEE_NAME as EMPLOYEE2_0_0_,
sale0_.SELL as SELL1_0_
from SALE sale0_
where sale0_.EMPLOYEE_ID=?
Hibernate:
select skill0_.EMPLOYEE_ID as EMPLOYEE1_0_0_,
skill0_.EMPLOYEE_NAME as EMPLOYEE2_0_0_,
skill0_.SKILLER as SKILLER2_0_
from SKILL skill0_
where skill0_.EMPLOYEE_ID=?
Hibernate:
select employee0_.EMPLOYEE_ID as EMPLOYEE1_0_0_,
employee0_.EMPLOYEE_NAME as EMPLOYEE2_0_0_,
employee0_.SELL as SELL1_0_,
employee0_.SKILLER as SKILLER2_0_,
employee0_.clazz_ as clazz_0_
from
(
select null as SELL,
null as SKILLER,
EMPLOYEE_ID,
EMPLOYEE_NAME,
0 as clazz_
from employees
union
select SELL,
null as SKILLER,
EMPLOYEE_ID,
EMPLOYEE_NAME,
1 as clazz_
from SALE
union
select null as SELL,
SKILLER,
EMPLOYEE_ID,
EMPLOYEE_NAME,
2 as clazz_
from SKILL
)
employee0_
where employee0_.EMPLOYEE_ID=?
查询结果:
Sale{id=1, name=sunliu, sell=300000}
Skill{id=2, name=wangwu, skiller=java}
Employee{id=3, name=lisi}
PS:使用union-subclass会为每一个类创建一个单独的表(抽象类除外),并且每一个表的主键id的值都是不一样的,这是由于主键生成器使用了hilo的缘故。使用hilo,Hibernate会创建一个名为hibernate_unique_key的表,这个表里存放着主键生成器使用的“高位“,Hibernate会在内存中生成一个低位,然后将高位和低位相加就得到了唯一的主键id,这样就可以保证每个表的id不会重复。
使用这种策略(指使用union-subclass会为每一个类创建一个单独的表)效率是比较低的。这个可以从第三个查询语句看出,这个语句很复杂,主要是它使用了三张表,它将三张表连接起来,使用子查询,用union这种方式将结果连接起来,看哪个不为空,不为空的就是要找的记录;相当于将三张表的记录连接起来,看哪个不为空,只要不为空的就是那条记录了,所以效率还不是很高。