hibernate 继承映射关系( SINGLE_TABLE)
三种继承映射关系。
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();
}
}
hibernate 继承映射关系( SINGLE_TABLE)的更多相关文章
- hibernate 继承映射关系( JOINED)
一个主表,其他的表每个都有自己的表来装填自己特有的部分,共同的部分就放在主表中. package com.bjsxt.hibernate; import javax.persistence.Ent ...
- hibernate 继承映射关系( TABLE_PER_CLASS)
Person,Student,Teacher各创建一个表,主键用一个中间表生成. 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 ...
随机推荐
- hdu5079
这道题的难点在于思考dp表示什么 首先可以令ans[len]表示白色子矩阵边长最大值大于等于len的方案数则ans[len]-ans[len+1]就是beautifulness为len的方案数 白色子 ...
- AC日记——Magazine Ad codeforces 803d
803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...
- (2)C# 创建ef oracle
1.NUGet下载 2.下载 Oracle Developer Tools for Visual Studio https://www.oracle.com/technetwork/develope ...
- 记一次对python反弹shell的分析
前言 昨天学习了反弹shell,对python弹shell产生了一些疑惑 python -c 'import socket,subprocess,os;s=socket.socket(socket.A ...
- opencv图像二值化的函数cvThreshold()。 cvAdaptiveThreshol
OpenCV中对图像进行二值化的关键函数——cvThreshold(). 函数功能:采用Canny方法对图像进行边缘检测 函数原型: void cvThreshold( const CvArr* sr ...
- pthread条件变量
pthread条件变量等待条件有两种方式:无条件等待pthread_cond_wait()和计时等待pthread_cond_timedwait(),其中计时等待方式如果在给定时刻前条件没有满足,则返 ...
- [xsy1515]小学生数学题
题意:求$\begin{align*}\left(\sum\limits_{i=1}^n\dfrac 1i\right)\%\ p^k\end{align*}$ 数学真是太可爱了== 直接推公式 设$ ...
- 【分类讨论】Codeforces Round #395 (Div. 2) D. Timofey and rectangles
D题: 题目思路:给你n个不想交的矩形并别边长为奇数(很有用)问你可以可以只用四种颜色给n个矩形染色使得相接触的 矩形的颜色不相同,我们首先考虑可不可能,我们分析下最多有几个矩形互相接触,两个时可以都 ...
- 【二分答案】【哈希表】【字符串哈希】bzoj2946 [Poi2000]公共串
二分答案,然后搞出hash值扔到哈希表里.期望复杂度O(n*log(n)). <法一>next数组版哈希表 #include<cstdio> #include<cstri ...
- 【KMP】BZOJ3670-[Noi2014]动物园
[题目大意][依然借用别人的概括]给定一个长为L的字符串(L<=100W),求一个num数组,num[i]表示长度为i的前缀中字符串S’的数量,其中S‘既是该前缀的前缀也是该前缀的后缀,且|S' ...