hibernate中继承映射的三种方式(SINGLE_TABLE,TABLE_PER_CLASS,JOINED)
2017-01-30 18:18
495 查看
1.在hibernate中继承映射的三种方式 SINGLE_TABLE,TABLE_PER_CLASS,JOINED
2. SINGLE_TABLE第一种方式
3. 我们以Person,Student,Teacher类来举例说明(Person是父类,其余两个是子类)
4.在Person父类中
5.其他两个子类中
6.生成表之后,我们会发现父类和子类的表是放在一块的。
7.TABLE_PER_CLASS的第二种方式
8.在父类Person中
9.在其他两个子类中只需要写上@Entity这个注解就可了
10.生成表我们可以看出数据库生成四张表
11.Joined第三种方式
12.Person父类中
13.其他两个类中,也是只需要@Entity注解即可。
14.生成表中,我们发现生成了三张表,person表,student表,teacher表。
15.附上测试代码如下:
package com.zhlk.model;
import java.util.Date;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.metamodel.relational.Schema;
import org.hibernate.sql.QuerySelect;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author lvguanghui date 2017-1-21
*/
public class HibernatORmappingTest {
private static SessionFactory sf = null;
@BeforeClass
public static void beforeClass() {
new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sf.close();
}
@Test
public void testUserSave() {
Student s=new Student();
s.setName("s1");
s.setScore(90);
Teacher t=new Teacher();
t.setName("t1");
t.setTitle("titleT1");
Session session = sf.getCurrentSession();
session.beginTransaction();
session.save(s);
session.save(t);
session.getTransaction().commit();
}
public static void main(String[] args) {
beforeClass();
}
}
2. SINGLE_TABLE第一种方式
3. 我们以Person,Student,Teacher类来举例说明(Person是父类,其余两个是子类)
4.在Person父类中
5.其他两个子类中
6.生成表之后,我们会发现父类和子类的表是放在一块的。
7.TABLE_PER_CLASS的第二种方式
8.在父类Person中
9.在其他两个子类中只需要写上@Entity这个注解就可了
10.生成表我们可以看出数据库生成四张表
11.Joined第三种方式
12.Person父类中
13.其他两个类中,也是只需要@Entity注解即可。
14.生成表中,我们发现生成了三张表,person表,student表,teacher表。
15.附上测试代码如下:
package com.zhlk.model;
import java.util.Date;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.metamodel.relational.Schema;
import org.hibernate.sql.QuerySelect;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author lvguanghui date 2017-1-21
*/
public class HibernatORmappingTest {
private static SessionFactory sf = null;
@BeforeClass
public static void beforeClass() {
new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sf.close();
}
@Test
public void testUserSave() {
Student s=new Student();
s.setName("s1");
s.setScore(90);
Teacher t=new Teacher();
t.setName("t1");
t.setTitle("titleT1");
Session session = sf.getCurrentSession();
session.beginTransaction();
session.save(s);
session.save(t);
session.getTransaction().commit();
}
public static void main(String[] args) {
beforeClass();
}
}
相关文章推荐
- hibernate继承关系的映射(single_table,class_per_table,joined)
- hibernate继承关系的映射(single_table,class_per_table,joined)
- hibernate继承关系的映射(single_table,class_per_table,joined)
- (8) ebj学习: Jpa的SINGLE_TABLE,JOINED,TABLE_PER_CLASS三种继承策略
- 在JPA中,实体继承关系的映射策略共有三种:单表继承策略(table per class)、Joined策略(table per subclass)和Table_PER_Class策略。
- Hibernate继承映射的三种方式
- 关于table per subclass继承映射的注意点
- hibernate继承映射[映射单独一个tablexml版本]实例【与19相同,方式不同,未验证】(二十)
- (11)映射继承关系二之每个类对应一张表(@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
- 继承映射(三)每个具体的类一张表TABLE_PER_CLASS
- hibernate 继承映射关系( TABLE_PER_CLASS)
- Hibernate入门07_继承映射02_joined方式
- hibernate笔记--继承映射关系的三种实现方式
- Hibernate三种继承映射方式的比较
- Hibernate 实体层设计--Table per subclass
- Hibernate类的继承使用joined-class实现
- Hibernate继承映射三种策略学习
- Hibernate继承映射方式之每个子类一张表
- hibernate三种常用继承映射的优缺点
- Hibernate 继承映射实现方式之每个具体类对应一张表