Hinbernate继承映射为4张表注解版【不太理解】(二十一)
2013-12-30 11:37
211 查看
1
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;
}
}
2
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;
}
}
3
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;
}
}
4
package com.bjsxt.hibernate;
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();
}
}
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;
}
}
2
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;
}
}
3
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;
}
}
4
package com.bjsxt.hibernate;
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();
}
}
相关文章推荐
- Hibernat映射set,map,list 一堆多,多对一 manytoone[继承映射注解版]实例(十九)
- Java - 谈一下你对继承映射的理解。
- Hibernate深入理解----06映射继承关系
- Hibernate使用注解,继承映射写法
- Hibernate4学习总结(3)--注解形式的基础映射,主键映射,基本属性映射,复合属性映射,继承映射。
- @注解_继承映射
- Hibernate学习(3) (继承映射,配置文件和注解版)
- 一个有点不太好理解的有关继承问题
- HIbernate继承映射
- 14 继承(一)(整个继承树映射到一张表)
- 关于php后台使用继承模板的理解
- hibernate映射关系理解
- 13_1理解基类中成员的访问限定符和派生类的继承方式
- springmvc注解的处理器映射器和适配器
- 深入理解Java:注解(Annotation)自定义注解入门
- 第十三周—【项目1】理解基类中成员的访问限定符和派生类的继承方式
- Hibernate基于注解方式的各种映射全面总结
- ZigBee的一些不太容易理解的术语
- SpringMVC注解汇总(二)-请求映射规则
- JPA实体继承的映射