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();
}
}
hibernate 继承映射关系( TABLE_PER_CLASS)的更多相关文章
- hibernate 继承映射关系( SINGLE_TABLE)
三种继承映射关系. 1,SINGLE_TABLE person student teacher 在一个表中,student和teacher继承自person,通过一个Discriminato ...
- hibernate 继承映射关系( JOINED)
一个主表,其他的表每个都有自己的表来装填自己特有的部分,共同的部分就放在主表中. package com.bjsxt.hibernate; import javax.persistence.Ent ...
- EF里的继承映射关系TPH、TPT和TPC的讲解以及一些具体的例子
本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF的默认的继承映射关系:一张表存放基类和子类的所有列,自动生成的discr ...
- Hibernate关联映射关系
Hibernate关联映射关系 一.双向一对多关联映射关系:当类与类之间建立了关联,就可以方便的从一个对象导航到另一个或另一组与它关联的对象(一对多双向关联和多对一双向关联是完全一样的) 1.1创建实 ...
- entity framework里的继承映射关系TPH、TPT和TPC
本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF的默认的继承映射关系:一张表存放基类和子类的所有列,自动生成的discr ...
- EF——继承映射关系TPH、TPT和TPC的讲解以及一些具体的例子 05 (转)
EF里的继承映射关系TPH.TPT和TPC的讲解以及一些具体的例子 本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF ...
- hibernate笔记--继承映射关系的三种实现方式
单表继承映射(一张表): 假设我们现在有三个类,关系如下: Person类有两个子类Student和Teacher,并且子类都具有自己独有的属性.这种实体关系在hibernate中可以使用单表的继承映 ...
- 【JavaEE】Hibernate继承映射,不用多态查询只查父表的方法
几个月前,我在博问里面发了一个问题:http://q.cnblogs.com/q/64900/,但是一直没有找到好的答案,关闭问题以后才自己解决了,在这里分享一下. 首先我重复一下场景,博问里面举的动 ...
- Hibernate继承映射(@Inheritance)
继承映射在 Annotation 中使用 @Inheritance 注解,并且需要使用 strategy 属性指定继承策略,继承策略有 SINGLE_TABLE.TABLE_PER_CLASS 和 J ...
随机推荐
- PowerDesigner用例图展示设置
powerdesigner用例图显示设置 powerdesigner中的绘图功能真是不敢恭维,折线半天弄不直,直线半天弄不弯. 1.修改显示设置 tools-->display preferen ...
- HDU 1754.I Hate It-结构体版线段树(单点更新+区间查询最值)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- POJ 3984 迷宫问题【BFS/路径记录/手写队列】
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义 ...
- Windows 10 作为无线显示器无法被搜索到
症状描述: Windows 10 的投影到此电脑功能失效,但是其它功能正常.同一网络,室友的电脑正常. 解决办法: 设备管理器启用“Microsoft Wi-Fi Direct Virtual Ada ...
- 【贪心】Codeforces Round #402 (Div. 2) C. Dishonest Sellers
按照b[i]-a[i],对物品从大到小排序,如果这个值大于零,肯定要立刻购买,倘若小于0了,但是没买够K个的话,也得立刻购买. #include<cstdio> #include<a ...
- Bootstrap-table自定义可编辑每页显示记录数
写在前面: 最近在做的person功能,由于后期系统中person人数较多,不利用查找person,故需求方将要求可以自己编辑每页显示的数目,而不是固定的写死每页显示的数目. 下面先来看下bootsr ...
- BUG YII2.0 cURL error 6: Could not resolve host:
BUG描述:登录直接显示 原因:服务器设置端口权限,或者DNS毛病 解决方案:只能去服务器端设置,配置端口 DNS: 修改dns 114.114.114.114 或者 8.8.8.8
- Arena | 用Excel设计的RPG游戏
文章目录 写在前面 支持的软件 下载地址 游戏界面截图 写在前面 你在用Excel做报表的时候,世界的某个角落,有位大神早就用它做出了一款RPG游戏--Arena.xlsm 加拿大大学生Cary Wa ...
- iOS9 HTTPS解决办法
iOS9引入了新特性App Transport Security (ATS).详情:App Transport Security (ATS) 新特性要求App内访问的网络必须使用HTTPS协议.但是现 ...
- iOS开发技巧——关闭Autoresizing开启Autolayout
在使用Autolayout时有时候会有莫名其妙的错误,是因为UIView是默认同时开启Autoresizing和Autolayout的. 但我们的Autolayout很容易和Autoresizing冲 ...