JPA 菜鸟教程 16 继承-联合子类-JOINED

联合子类策略

这种情况下子类的字段被映射到各自的表中,这些字段包括父类中的字段,并执行一个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

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
**您的支持将鼓励我继续创作!谢谢! **

微信打赏
微信打赏

支付宝打赏
支付宝打赏

推荐阅读更多精彩内容

  • 继承映射策略 一个类继承结构一个表的策略,最终只生成一个表,这是继承映射的默认策略。 举例 如果实体类Teache...
    JeGe阅读 1,080评论 0 0
  • 2017年8月21日 我原本只想简单记录一下springboot中应用Jpa的简单操作。不想由于hibernate...
    行者N阅读 6,153评论 0 23
  • 本文中我们介绍并比较两种最流行的开源持久框架:iBATIS和Hibernate,我们还会讨论到Java Persi...
    大同若鱼阅读 4,148评论 4 27
  • 嗨,大家好!我是辰辰,你完全可以叫我小辰辰。因为我还特别小,虽然用的尿不湿的型号从NB升级为S号,但我也才43天而...
    小丫屠阅读 190评论 2 1
  • 阳光从南窗里大大方方地爬进来,刚倒的一杯水在阳光里热气 腾腾,旁边放着一首首古琴曲,我坐在周末的午后码字。 今天我...
    夏虫爱语冰阅读 1,415评论 0 0