Hibernate

Posted 57容杰龙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate相关的知识,希望对你有一定的参考价值。

注解高级(原文再续书接上一回)

7.继承映射

第一种:InheritanceType.JOINED 查询时会出现很多join语句.

 1 package com.rong.entity.joined;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.GenerationType;
 6 import javax.persistence.Id;
 7 import javax.persistence.Inheritance;
 8 import javax.persistence.InheritanceType;
 9 import javax.persistence.Table;
10 
11 @Entity
12 @Table(name="person")
13 //所有的持久化都会生成表(子类表会引用父类表的主键列)
14 //每个类都会生成一张表
15 //子类的表中会默认引用父类中的id
16 @Inheritance(strategy=InheritanceType.JOINED)
17 public class Person {
18     @Id
19     @GeneratedValue(strategy=GenerationType.AUTO)
20     private int id;
21     private String name;
22     private int age;
23     public int getId() {
24         return id;
25     }
26     public void setId(int id) {
27         this.id = id;
28     }
29     public String getName() {
30         return name;
31     }
32     public void setName(String name) {
33         this.name = name;
34     }
35     public int getAge() {
36         return age;
37     }
38     public void setAge(int age) {
39         this.age = age;
40     }
41     @Override
42     public String toString() {
43         return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
44     }
45 }
 1 package com.rong.entity.joined;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.Table;
 5 
 6 @Entity
 7 @Table(name="student")
 8 public class Student extends Person{
 9     private String number;
10 
11     public String getNumber() {
12         return number;
13     }
14 
15     public void setNumber(String number) {
16         this.number = number;
17     }
18 }

 1 package com.rong.test;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.Transaction;
 6 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
 7 import org.hibernate.cfg.Configuration;
 8 import org.hibernate.service.ServiceRegistry;
 9 import org.junit.Test;
10 
11 import com.rong.entity.joined.Person;
12 import com.rong.entity.joined.Student;
13 
14 public class HibernateTest {
15     @Test
16     public void test1(){
17         Configuration configuration=new Configuration().configure();
18         ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
19         SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
20         Session session = sessionFactory.openSession();
21         Transaction transaction = session.beginTransaction();
22         //插入
23         Student student=new Student();
24         student.setAge(18);
25         student.setName("悠悠");
26         student.setNumber("654321");
27         session.save(student);
28         //查询
29         Person person=(Person)session.get(Person.class, 1);
30         System.out.println(person);
31         Student stu=(Student) session.get(Student.class, 1);
32         System.out.println(stu.getId()+":"+stu.getName()+":"+stu.getNumber());
33         transaction.commit();
34         session.close();
35         sessionFactory.close();
36     }
37 }

第二种:InheritanceType.SINGLE_TABLE 所有子类属性中不能加非空约束

 

 1 package com.rong.entity.singletable;
 2 
 3 import javax.persistence.DiscriminatorColumn;
 4 import javax.persistence.DiscriminatorType;
 5 import javax.persistence.DiscriminatorValue;
 6 import javax.persistence.Entity;
 7 import javax.persistence.GeneratedValue;
 8 import javax.persistence.GenerationType;
 9 import javax.persistence.Id;
10 import javax.persistence.Inheritance;
11 import javax.persistence.InheritanceType;
12 import javax.persistence.Table;
13 
14 @Entity
15 @Table(name="person")
16 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)// 单个表
17 @DiscriminatorColumn(name="dc",discriminatorType=DiscriminatorType.INTEGER)//辨别者列
18 @DiscriminatorValue(value="0")// 辨别者列值
19 public class Person {
20     @Id
21     @GeneratedValue(strategy=GenerationType.AUTO)
22     private int id;
23     private String name;
24     private int age;
25     public int getId() {
26         return id;
27     }
28     public void setId(int id) {
29         this.id = id;
30     }
31     public String getName() {
32         return name;
33     }
34     public void setName(String name) {
35         this.name = name;
36     }
37     public int getAge() {
38         return age;
39     }
40     public void setAge(int age) {
41         this.age = age;
42     }
43     @Override
44     public String toString() {
45         return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
46     }
47 }
 1 package com.rong.entity.singletable;
 2 
 3 import javax.persistence.DiscriminatorValue;
 4 import javax.persistence.Entity;
 5 import javax.persistence.Table;
 6 
 7 @Entity
 8 @Table(name="student")
 9 @DiscriminatorValue(value="1")// 辨别者列值
10 public class Student extends Person{
11     private String number;
12 
13     public String getNumber() {
14         return number;
15     }
16 
17     public void setNumber(String number) {
18         this.number = number;
19     }
20 }
 1 package com.rong.test;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.Transaction;
 6 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
 7 import org.hibernate.cfg.Configuration;
 8 import org.hibernate.service.ServiceRegistry;
 9 import org.junit.Test;
10 
11 import com.rong.entity.singletable.Person;
12 import com.rong.entity.singletable.Student;
13 
14 public class HibernateTest {
15     @Test
16     public void test1(){
17         Configuration configuration=new Configuration().configure();
18         ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
19         SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
20         Session session = sessionFactory.openSession();
21         Transaction transaction = session.beginTransaction();
22         //插入
23         Student student=new Student();
24         student.setAge(16);
25         student.setName("晓晓");
26         student.setNumber("3824");
27         session.save(student);
28         Person person=new Person();
29         person.setAge(19);
30         person.setName("达达");
31         session.save(person);
32         //查询
33         Person per=(Person)session.get(Person.class, 2);
34         System.out.println(per);
35         Student stu=(Student) session.get(Student.class, 1);
36         System.out.println(stu.getId()+":"+stu.getName()+":"+stu.getNumber());
37         transaction.commit();
38         session.close();
39         sessionFactory.close();
40     }
41 }

 

第三种:InheritanceType.TABLE_PER_CLASS 这种策略主键不能用自增长.查询时会出现union运算.

 1 package com.rong.entity.tableperclass;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.Id;
 5 import javax.persistence.Inheritance;
 6 import javax.persistence.InheritanceType;
 7 import javax.persistence.Table;
 8 
 9 @Entity
10 @Table(name="person")
11 //所有的持久化都会生成表(子类会把父类中的属性继承过来生成在自己的表中)
12 @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
13 public class Person {
14     @Id
15     private int id;
16     private String name;
17     private int age;
18     public int getId() {
19         return id;
20     }
21     public void setId(int id) {
22         this.id = id;
23     }
24     public String getName() {
25         return name;
26     }
27     public void setName(String name) {
28         this.name = name;
29     }
30     public int getAge() {
31         return age;
32     }
33     public void setAge(int age) {
34         this.age = age;
35     }
36     @Override
37     public String toString() {
38         return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
39     }
40 }
 1 package com.rong.entity.tableperclass;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.Table;
 5 
 6 @Entity
 7 @Table(name="student")
 8 public class Student extends Person{
 9     private String number;
10 
11     public String getNumber() {
12         return number;
13     }
14 
15     public void setNumber(String number) {
16         this.number = number;
17     }
18 }
 1 package com.rong.test;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.Transaction;
 6 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
 7 import org.hibernate.cfg.Configuration;
 8 import org.hibernate.service.ServiceRegistry;
 9 import org.junit.Test;
10 
11 import com.rong.entity.tableperclass.Student;
12 public class HibernateTest {
13     @Test
14     public void test1(){
15         Configuration configuration=new Configuration().configure();
16         ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
17         SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
18         Session session = sessionFactory.openSession();
19         Transaction transaction = session.beginTransaction();
20         //插入
21         Student student=new Student();
22         student.setId(1);
23         student.setAge(20);
24         student.setName("扣扣");
25         student.setNumber("1224832");
26         session.save(student);
27         //查询
28         Student stu=(Student) session.get(Student.class, 1);
29         System.out.println(stu.getId()+":"+stu.getName()+":"+stu.getNumber()
30                             +":"+stu.getAge());
31         transaction.commit();
32         session.close();
33         sessionFactory.close();
34     }
35 }

 

以上是关于Hibernate的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate的HQL多表查询

使用反射在外部JAR / CLASS上调用包含Hibernate事务的方法(Java EE)

Hibernate CriteriaQuery where - ManyToOne 字段

Hibernate + MySQL:如何为数据库和表设置编码 utf-8

hibernate在使用getCurrentSession时提示no session found for current thread

Java类型相互转换byte[]类型,blob类型