用一张
每一个类一张表
建立外键
第一种 一张总表
Person
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; } }
Student.
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; } }
Teacher
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; } }
testsave
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(); } }
testload
@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(); }
这种方式会产生大量冗余字段 比如存Teacher会导致score为空等 适合数据量少使用
第二种方式 每一个类一个表
Person
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; } }
Student
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; } }
teacher
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; } }
testsave
@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(); }
testload
@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); //会从teacher和student中取出数据 联合生成一个表,然后从里面找出id为2的 System.out.println(p.getName()); session.getTransaction().commit(); session.close();
第三种方式 外键关联
Person
package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; @Entity @Inheritance(strategy=InheritanceType.JOINED) 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; } }
Teache
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; } }
student
package com.bjsxt.hibernate; import javax.persistence.Entity; @Entity public class Student extends Person { private int score; public int getScore() { return score; } //Person p = Person(load(1)); public void setScore(int score) { this.score = score; } }
testsave
package com.bjsxt.hibernate; import javax.persistence.Entity; @Entity public class Student extends Person { private int score; public int getScore() { return score; } //Person p = Person(load(1)); public void setScore(int score) { this.score = score; } }
testload
@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(); }
一般来说 用第三种和第一种比较多