博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hibernate 继承映射关系( TABLE_PER_CLASS)
阅读量:5938 次
发布时间:2019-06-19

本文共 2837 字,大约阅读时间需要 9 分钟。

Person,Student,Teacher各创建一个表,主键用一个中间表生成。

 

package com.bjsxt.hibernate;

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.TableGenerator;

@Entity

@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@TableGenerator(
        name="t_gen",
        table="t_gen_table",
        pkColumnName="t_pk",
        valueColumnName="t_value",
        pkColumnValue="person_pk",
        initialValue=1,
        allocationSize=1
        )
public class Person {
    private int id;
    private String name;
   
    @Id
    @GeneratedValue(generator="t_gen", strategy=GenerationType.TABLE)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

 

package com.bjsxt.hibernate;

import javax.persistence.Entity;

@Entity

public class Student extends Person {
   
    private int score;

    public int getScore() {

        return score;
    }

    public void setScore(int score) {

        this.score = score;
    }
   
}

 

package com.bjsxt.hibernate;

import javax.persistence.Entity;

@Entity

public class Teacher extends Person {
    private String title;

    public String getTitle() {

        return title;
    }

    public void setTitle(String title) {

        this.title = title;
    }

   

}

 

测试类:

package com.bjsxt.hibernate;

import java.util.Map;

import org.hibernate.Session;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateORMappingTest {

    private static SessionFactory sessionFactory;
   
    @BeforeClass
    public static void beforeClass() {
        new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
        sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    }
    @AfterClass
    public static void afterClass() {
        sessionFactory.close();
    }
   
    @Test
    public void testSave() {
        Student s = new Student();
        s.setName("s1");
        s.setScore(80);
        Teacher t = new Teacher();
        t.setName("t1");
        t.setTitle("中级");
       
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(s);
        session.save(t);
        session.getTransaction().commit();
        session.close();
    }
    @Test
    public void testLoad() {
        testSave();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        Student s = (Student)session.load(Student.class, 1);
        System.out.println(s.getScore());
        Person p = (Person)session.load(Person.class, 2);
        System.out.println(p.getName());
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testSchemaExport() {
        new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
    }
   
   
    public static void main(String[] args) {
        beforeClass();
    }
}

转载地址:http://quvtx.baihongyu.com/

你可能感兴趣的文章
MFC_Combo_Box(组合框)控件的用法
查看>>
ldap
查看>>
我的友情链接
查看>>
CentOS 7更改网卡名称
查看>>
Yum软件仓库配置
查看>>
linux 压缩与解压总结
查看>>
mysql脚本1064 - You have an error in your SQL syntax; check the manual
查看>>
nessus 本地扫描(一)
查看>>
linux服务器磁盘陈列
查看>>
交换机配置模式
查看>>
python----tcp/ip http
查看>>
我的友情链接
查看>>
第一本docker书学习笔记1-3章
查看>>
一個典型僵尸網絡淺析
查看>>
vmware克隆Centos6.4虚拟机网卡无法启动问题
查看>>
dba学习
查看>>
asterisk配置
查看>>
GA操作步骤和技巧(二)——用户行为分析
查看>>
shell中while循环里使用ssh的注意事项
查看>>
SHELL获取计算机外网ip的几种写法
查看>>