JPA 系列教程17-继承-独立表-TABLE_PER_CLASS

时间:2021-11-04 15:11:22

PerTable策略

每个具体的类一个表的策略

举例

这种映射策略每个类都会映射成一个单独的表,类的所有属性,包括继承的属性都会映射成表的列。

这种映射策略的缺点是:对多态关系的支持有限,当查询涉及到类继承结构时通常需要发起SQL UNION查询。

配置

注解为:@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

ddl语句

CREATE TABLE `hibernate_sequences` (
`sequence_name` varchar(255) DEFAULT NULL,
`sequence_next_hi_value` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `t_person` (
`id` bigint(20) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `t_teacher` (
`id` bigint(20) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `t_student` (
`id` bigint(20) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`school` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Person

package com.jege.jpa.extend;

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.Table; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:父类
*/
@Entity
@Table(name = "t_person")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Person {
@Id
// 主键生成方式不能是IDENTITY
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
private String name; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

Teacher

package com.jege.jpa.extend;

import javax.persistence.Entity;
import javax.persistence.Table; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:子类
*/
@Entity
@Table(name = "t_teacher")
public class Teacher extends Person {
private String address; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} }

Student

package com.jege.jpa.extend;

import javax.persistence.Entity;
import javax.persistence.Table; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:父子类
*/
@Entity
@Table(name = "t_student")
public class Student extends Person {
private String school; public String getSchool() {
return school;
} public void setSchool(String school) {
this.school = school;
} }

MainTest

package com.jege.jpa.extend;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:继承测试
*/
public class MainTest {
private static EntityManagerFactory entityManagerFactory = null;
private EntityManager entityManager = null; @BeforeClass
public static void setUpBeforeClass() throws Exception {
entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");
} @Before
public void setUp() throws Exception {
entityManager = entityManagerFactory.createEntityManager();
} @Test
public void persist() {
Person person = new Person();
person.setName("jege"); Teacher teacher = new Teacher();
teacher.setName("仓老师");
teacher.setAddress("北京"); Student student = new Student();
student.setName("机械师");
student.setSchool("上海"); entityManager.getTransaction().begin();
entityManager.persist(student);
entityManager.persist(teacher);
entityManager.persist(person);
entityManager.getTransaction().commit();
} @Test
public void find() {
persist(); entityManager.clear();
Student student = entityManager.find(Student.class, 1L);
System.out.println(student.getSchool());
} @After
public void tearDown() throws Exception {
if (entityManager != null && entityManager.isOpen())
entityManager.close();
} @AfterClass
public static void tearDownAfterClass() throws Exception {
if (entityManagerFactory != null && entityManagerFactory.isOpen())
entityManagerFactory.close();
}
}

其他关联项目

源码地址

https://github.com/je-ge/jpa

如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!

JPA 系列教程17-继承-独立表-TABLE_PER_CLASS

JPA 系列教程17-继承-独立表-TABLE_PER_CLASS