JPA 系列教程16-继承-联合子类-JOINED

时间:2023-12-26 22:38:49

联合子类策略

这种情况下子类的字段被映射到各自的表中,这些字段包括父类中的字段,并执行一个join操作来实例化子类。

举例

如果实体类Teacher继承实体类Person,实体类Student也继承自实体Person,

会映射成3个表,子表不包含父表的属性,子表只有子类的属性,子表主键外键关联父表的id

这种策略超类会被映射成一个单独的表,每个子类也会映射成一个单独的表。子类对应的表中只包括自身属性对应的字段,默认情况下使用主键作为超类对应的表的外键。

这种策略对于实体间的多态关系提供了很好的支持。但缺点是实例化子类实例时需要一个或多个表的关联操作。在深层次的继承结构中,这会导致性能很低。

配置

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

ddl语句

CREATE TABLE `t_person` (
`type` int(11) NOT NULL,
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; CREATE TABLE `t_teacher` (
`address` varchar(255) DEFAULT NULL,
`id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_8ik5mbfy94c9cdklxfo2wqgg9` FOREIGN KEY (`id`) REFERENCES `t_person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `t_student` (
`school` varchar(255) DEFAULT NULL,
`id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_bdp2c9ntgnbd0ydfj1fwqa7l2` FOREIGN KEY (`id`) REFERENCES `t_person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Person

package com.jege.jpa.extend;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
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.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
public class Person {
@Id
@GeneratedValue
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 系列教程16-继承-联合子类-JOINED

JPA 系列教程16-继承-联合子类-JOINED