1. 程式人生 > >JPA 菜鳥教程 15 繼承-一個表-SINGLE_TABLE

JPA 菜鳥教程 15 繼承-一個表-SINGLE_TABLE

column turn rate pre school fill 技術 一個表 tor

原地址: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 [email protected]
 * @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 [email protected]
 * @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 [email protected]
 * @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 [email protected]
 * @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 菜鳥教程 15 繼承-一個表-SINGLE_TABLE