JPA 菜鸟教程 15 继承-一个表-SINGLE_TABLE

原地址:http://blog.csdn.net/JE_GE/article/details/53678422

继承映射策略

一个类继承结构一个表的策略,最终只生成一个表,这是继承映射的默认策略。

举例

如果实体类Teacher继承实体类Person,实体类Student也继承自实体Person,那么只会映射成一个表,这个表中包括了实体类Person、Teacher、Student中所有的字段

这种策略中,一个继承结构中的所有类都被映射到一个表中。该表中有一列被当作“discriminator列”,即使用该列来识别某行数据属于某个指定的子类实例。 
这种映射策略对实体和涉及类继承结构的查询的多态系统提供了很好的支持。但缺点是要求与子类的指定状态对应的列可以为空。

配置

JPA使用一个叫做“discriminator列”来区分某一行数据是应该映射成哪个实体。 
注解为:@Inheritance(strategy = InheritanceType.SINGLE_TABLE)

ddl语句

CREATE TABLE `t_person` (
  `type` varchar(31) NOT NULL,
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `school` varchar(255) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

保存后的数据

这里写图片描述

Person

package com.jege.jpa.extend;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
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.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
@DiscriminatorValue("1")
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.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:子类
 */
@Entity
@Table(name = "t_teacher")
@DiscriminatorValue("2")
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.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:子类
 */
@Entity
@Table(name = "t_student")
@DiscriminatorValue("3")
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

posted @ 2017-11-25 21:40  bcombetter  阅读(672)  评论(0编辑  收藏  举报