zoukankan      html  css  js  c++  java
  • JPA 系列教程17-继承-独立表-TABLE_PER_CLASS

    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

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

  • 相关阅读:
    hdu 1715
    hdu 1370
    hdu 2370
    hdu 1393
    hdu 1564
    hdu 1720
    hdu 1342
    SQL技巧(sp_procedure_params_rowset,SQL中设置数据值为null)
    Control ‘dg’ of type 'GridView' must be placed inside a form tag with runat=server
    GridView事件中获取rowIndex值
  • 原文地址:https://www.cnblogs.com/je-ge/p/6216475.html
Copyright © 2011-2022 走看看