Hibernate 映射继承

Posted kika

tags:

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

所有项目导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java

第一节:每个具体类对应一个表

 Image.java

 1 package com.wishwzp.model;
 2 
 3 public abstract class Image {
 4 
 5     private int id;
 6     private String imageName;
 7     private Student student;
 8     
 9     public int getId() {
10         return id;
11     }
12     public void setId(int id) {
13         this.id = id;
14     }
15     public String getImageName() {
16         return imageName;
17     }
18     public void setImageName(String imageName) {
19         this.imageName = imageName;
20     }
21     public Student getStudent() {
22         return student;
23     }
24     public void setStudent(Student student) {
25         this.student = student;
26     }
27 }

 

WorkImage.java

1 package com.wishwzp.model;
2 
3 public class WorkImage extends Image{
4 
5 }

LifeImage.java

1 package com.wishwzp.model;
2 
3 public class LifeImage extends Image{
4 
5 }

Student.java

 1 package com.wishwzp.model;
 2 
 3 import java.util.Set;
 4 
 5 public class Student {
 6 
 7     private int id;
 8     private String name;
 9     private Set<Image> images;
10     
11     public int getId() {
12         return id;
13     }
14     public void setId(int id) {
15         this.id = id;
16     }
17     public String getName() {
18         return name;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     public Set<Image> getImages() {
24         return images;
25     }
26     public void setImages(Set<Image> images) {
27         this.images = images;
28     }
29 }

 

 hibernate.cfg.xml

 1 <?xml version=\'1.0\' encoding=\'utf-8\'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7 
 8     <session-factory>
 9 
10         <!--数据库连接设置 -->
11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
13         <property name="connection.username">root</property>
14         <property name="connection.password">123456</property>
15 
16        
17         <!-- 方言 -->
18         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
19     
20         <!-- 控制台显示SQL -->
21         <property name="show_sql">true</property>
22 
23         <!-- 自动更新表结构 -->
24         <property name="hbm2ddl.auto">update</property>
25         
26           <mapping resource="com/wishwzp/model/Student.hbm.xml"/>
27           <mapping resource="com/wishwzp/model/LifeImage.hbm.xml"/>
28           <mapping resource="com/wishwzp/model/WorkImage.hbm.xml"/>
29 
30     </session-factory>
31 
32 </hibernate-configuration>

 

 StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Image;
15 import com.wishwzp.model.Image2;
16 import com.wishwzp.model.Image3;
17 import com.wishwzp.model.Student2;
18 import com.wishwzp.model.Student3;
19 import com.wishwzp.util.HibernateUtil;
20 
21 public class StudentTest {
22 
23     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
24     private Session session;
25     
26     @Before
27     public void setUp() throws Exception {
28         session=sessionFactory.openSession(); // 生成一个session
29         session.beginTransaction(); // 开启事务
30     }
31 
32     @After
33     public void tearDown() throws Exception {
34          session.getTransaction().commit(); // 提交事务
35          session.close(); // 关闭session
36     }
37 
38     @Test
39     public void testGetAllImages(){
40         
41     }
42 }

 

 WorkImage.java和LifeImage.java继承了 Image.java,我们在数据库里面可以看出来的。

 

这里我们在向数据库里面插入一些数据。。。

 

 向t_student表中插入stuName为张三:

 向t_lifeimage表中插入imageName:

向t_workimage表中插入imageName:

 

然后我们查询这些数据:

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.wishwzp.model.Image;
15 import com.wishwzp.model.Image2;
16 import com.wishwzp.model.Image3;
17 import com.wishwzp.model.Student2;
18 import com.wishwzp.model.Student3;
19 import com.wishwzp.util.HibernateUtil;
20 
21 public class StudentTest {
22 
23     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
24     private Session session;
25     
26     @Before
27     public void setUp() throws Exception {
28         session=sessionFactory.openSession(); // 生成一个session
29         session.beginTransaction(); // 开启事务
30     }
31 
32     @After
33     public void tearDown() throws Exception {
34          session.getTransaction().commit(); // 提交事务
35          session.close(); // 关闭session
36     }
37 
38     @Test
39     public void testGetAllImages(){
40         List<Image> imageList=new ArrayList<Image>();
41         int stuId=1;
42         List<Image> lifeImageList=(List<Image>)session.createQuery("from LifeImage l where l.student.id="+stuId).list();
43         imageList.addAll(lifeImageList);
44         List<Image> workImageList=(List<Image>)session.createQuery("from WorkImage w where w.student.id="+stuId).list();
45         imageList.addAll(workImageList);
46         Iterator it=imageList.iterator();
47         while(it.hasNext()){
48             Image image=(Image)it.next();
49             System.out.println(image.getImageName());
50         }
51     }
52 }

 

控制台显示:

 

 

第二节:根类对应一个表

 image2.java

 1 package com.wishwzp.model;
 2 
 3 public class Image2 {
 4 
 5     private int id;
 6     private String imageName;
 7     private String imageType;
 8     private Student2 student;
 9     
10     public int getId() {
11         return id;
12     }
13     public void setId(int id) {
14         this.id = id;
15     }
16     public String getImageName() {
17         return imageName;
18     }
19     public void setImageName(String imageName) {
20         this.imageName = imageName;
21     }
22     public Student2 getStudent() {
23         return student;
24     }
25     public void setStudent(Student2 student) {
26         this.student = student;
27     }
28     public String getImageType() {
29         return imageType;
30     }
31     public void setImageType(String imageType) {
32         this.imageType = imageType;
33     }
34 }

LifeImage2.java

1 package com.wishwzp.model;
2 
3 public class LifeImage2 extends Image2{
4 
5 }

WorkImage2.java

1 package com.wishwzp.model;
2 
3 public class WorkImage2 extends Image2{
4 
5 }

Student2.java

 1 package com.wishwzp.model;
 2 
 3 import java.util.Set;
 4 
 5 public class Student2 {
 6 
 7     private int id;
 8     private String name;
 9     private Set<Image2> images;
10     
11     public int getId() {
12         return id;
13     }
14     public void setId(int id) {
15         this.id = id;
16     }
17     public String getName() {
18         return name;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     public Set<Image2> getImages() {
24         return images;
25     }
26     public void setImages(Set<Image2> images) {
27         this.images = images;
28     }
29     
30     
31     
32 }

 

image2.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Image2" table="t_image2">
 9         <id name="id" column="imageId">
10             <generator class="native"></generator>
11         </id>
12         
13         <discriminator column="imageType" type="string"></discriminator>
14         <property name="imageName" column="imageName"></property>
15         
16         <many-to-one name="student" column="stuId" class="com.wishwzp.model.Student2"></many-to-one>
17         
18         <subclass name="com.wishwzp.model.LifeImage2" discriminator-value="li"></subclass>
19         <subclass name="com.wishwzp.model.WorkImage2" discriminator-value="wi"></subclass>
20     </class>
21 
22 </hibernate-mapping>

 

 Student2.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Student2" table="t_student2">
 9         <id name="id" column="stuId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name" column="stuName"></property>
14         
15         <set name="images">
16             <key column="stuId"></key>
17             <one-to-many class="com.wishwzp.model.Image2"/>
18         </set>
19     </class>
20 
21 </hibernate-mapping>

 

hibernate.cfg.xml

 1 <?xml version=\'1.0\' encoding=\'utf-8\'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7 
 8     <session-factory>
 9 
10         <!--数据库连接设置 -->
11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
13         <property name="connection.username">root</property>
14         <property name="connection.password">123456</property>
15 
16        
17         <!-- 方言 -->
18         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
19     
20         <!-- 控制台显示SQL -->
21         <property name="show_sql">true</property>
22 
23         <!-- 自动更新表结构 -->
24         <property name="hbm2ddl.auto">update</property>
25 
26           <mapping resource="com/wishwzp/model/Student2.hbm.xml"/>
27           <mapping resource="com/wishwzp/model/Image2.hbm.xml"/>
28           
29 
30     </session-factory>
31 
32 </hibernate-configuration>

 

 StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Iterator;
 5 import java.util.List;
 6 import java.util.Set;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.junit.After;
11 import org.junit.Before;
12 以上是关于Hibernate 映射继承的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate中的继承映射

一口一口吃掉Hibernate——继承映射

SSH快速进阶——Hibernate继承映射:每个类映射一张表

Hibernate 映射继承

hibernate笔记--继承映射关系的三种实现方式

Hibernate的继承映射