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

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

三种继承映射关系。

 

1,SINGLE_TABLE

 

person student  teacher 在一个表中,student和teacher继承自person,通过一个DiscriminatorValue的内容来标识该条内容是student还是teacher。

 

package com.bjsxt.hibernate;

import javax.persistence.DiscriminatorColumn;

import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

@Entity

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator", discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("person")
public class Person {
    private int id;
    private String name;
   
    @Id
    @GeneratedValue
    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.DiscriminatorValue;

import javax.persistence.Entity;

@Entity

@DiscriminatorValue("student")
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.DiscriminatorValue;

import javax.persistence.Entity;

@Entity

@DiscriminatorValue("teacher")
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/3480901.html

你可能感兴趣的文章
进击吧!阶乘——大数乘法
查看>>
安卓学习资料推荐-25
查看>>
Mysql数据库备份和还原常用的命令
查看>>
关于退出当前页面在火狐的一些问题
查看>>
【项目实施】项目考核标准
查看>>
spring-aop AnnotationAwareAspectJAutoProxyCreator类
查看>>
经典入门_排序
查看>>
Redis Cluster高可用集群在线迁移操作记录【转】
查看>>
二、spring中装配bean
查看>>
VIM工具
查看>>
javascript闭包
查看>>
@Column标记持久化详细说明
查看>>
创建本地yum软件源,为本地Package安装Cloudera Manager、Cloudera Hadoop及Impala做准备...
查看>>
mysql8.0.13下载与安装图文教程
查看>>
站立会议08(冲刺2)
查看>>
url查询参数解析
查看>>
http://coolshell.cn/articles/10910.html
查看>>
[转]jsbsim基础概念
查看>>
DIV和SPAN的区别
查看>>
第一次使用cnblogs
查看>>