对于继承映射表的设计有三种:
以person、student、teacher说明:
一、继承关系树的每个类对应一个表(joined类型):
Person.java:
package com.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Person {
private int id;
private String name;
@Id
@GeneratedValue
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;
}
}
Student.java:
package com.bean;
import javax.persistence.Entity;
@Entity
public class Student extends Person {
private String num;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
Teacher.java:
package com.bean;
import javax.persistence.Entity;
@Entity
public class Teacher extends Person {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
测试类:
package com.test;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import com.bean.Person;
import com.bean.Student;
import com.bean.Teacher;
public class Test_W_H {
public static void main(String []args) {
Configuration cfg = new Configuration();
cfg.configure(); //加载hibernate配置文件
SessionFactory sessionFactory = cfg.buildSessionFactory(new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry());
Session session = sessionFactory.getCurrentSession();
// Student student = new Student();
// student.setName("张三");
// student.setNum("20151080300");
//
// Teacher teacher = new Teacher();
// teacher.setName("Mr.sun");
// teacher.setTitle("教授");
session.beginTransaction(); //开启事务
// session.save(student);
// session.save(teacher);
Student student = (Student)session.load(Student.class, 1);
System.out.println(student.getName());
Person person = (Person)session.load(Person.class, 2);
System.out.println(person.getName());
session.getTransaction().commit(); // 事务提交
System.out.println("the end");
}
}
Hibernate生成的Hql语句:
Hibernate:
select
student0_.id as id0_0_,
student0_1_.name as name0_0_,
student0_.num as num2_0_
from
Student student0_
inner join
Person student0_1_
on student0_.id=student0_1_.id
where
student0_.id=?
张三
Hibernate:
select
person0_.id as id0_0_,
person0_.name as name0_0_,
person0_1_.title as title1_0_,
person0_2_.num as num2_0_,
case
when person0_1_.id is not null then 1
when person0_2_.id is not null then 2
when person0_.id is not null then 0
end as clazz_0_
from
Person person0_
left outer join
Teacher person0_1_
on person0_.id=person0_1_.id
left outer join
Student person0_2_
on person0_.id=person0_2_.id
where
person0_.id=?
Mr.sun
----------------------------------------------------------------------------------------------
二、继承关系树的根类对应一个表(single类型)
Person.java:
package com.bean;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator",discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("person")
public class Person {
private int id;
private String name;
@Id
@GeneratedValue
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;
}
}
Student.java:
package com.bean;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("student")
public class Student extends Person {
private String num;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
Teacher.java:
package com.bean;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("teacher")
public class Teacher extends Person {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
-------------------------------------------------------------------------------------------
三、(TABLE_PER_CLASS)
Person.java:
package com.bean;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.TableGenerator;
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@TableGenerator(
name="person",
table="GEN_PERSON",
pkColumnName="P_key",
valueColumnName="P_value",
pkColumnValue="person_pk",
initialValue=1,
allocationSize=1
)
public class Person {
private int id;
private String name;
@Id
@GeneratedValue(strategy=GenerationType.TABLE,generator="person")
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;
}
}
Student.java:
package com.bean;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
public class Student extends Person {
private String num;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
Teacher.java:
package com.bean;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
public class Teacher extends Person {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}