hibernate 继承映射关系( TABLE_PER_CLASS)

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();
    }
}

转载于:https://www.cnblogs.com/flying607/p/3483335.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值