本文地址:www.cnblogs.com/rongmuping/articles/rongmuping_hibernate5_3.html

一、Mapping

1.HStudentEntity.hbm.xml

 1 <?xml version='1.0' encoding='utf-8'?>
 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 <hibernate-mapping package="com.sdkj.hibernate.domain">
 6 
 7     <class name="HStudentEntity" table="h_student" schema="678">
 8         <id name="sid" type="int">
 9             <column name="sid"></column>
10             <generator class="native"></generator>
11         </id>
12         <property name="sname" column="sname"/>
13         <!-- 配置多对一 hTeacherEntity对应实体类get -->
14         <many-to-one name="hTeacherEntity" class="HTeacherEntity">
15             <column name="tid"></column>
16         </many-to-one>
17     </class>
18 </hibernate-mapping>

2.HTeacherEntity.hbm.xml

 1 <?xml version='1.0' encoding='utf-8'?>
 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 <hibernate-mapping package="com.sdkj.hibernate.domain">
 6 
 7     <class name="HTeacherEntity" table="h_teacher" schema="678">
 8         <id name="tid" type="int">
 9             <column name="tid"></column>
10             <generator class="native"></generator>
11         </id>
12         <property name="tname" column="tname"/>
13 
14         <set name="studentEntities" table="h_student" inverse="true" order-by="sid asc">
15             <!-- studentEntities对应实体类get /
16             inverse = true 设置不维护外键关系,设置此项可减少冗余的访问数据库次数 /
17             order by = tid asc/desc 设置拼接在sql语句最后的排序  -->
18             <key>
19                 <column name="tid"/>
20             </key>
21             <one-to-many class="com.sdkj.hibernate.domain.HStudentEntity"/>
22         </set>
23     </class>
24 </hibernate-mapping>

 

二、Java Test类

1.TestDXManyToOne

 1 package com.sdkj.hibernate.controller;
 2 
 3 import com.sdkj.hibernate.domain.HStudentEntity;
 4 import com.sdkj.hibernate.domain.HTeacherEntity;
 5 import com.sdkj.hibernate.util.CommonUtil;
 6 import org.hibernate.Session;
 7 import org.hibernate.SessionFactory;
 8 import org.hibernate.Transaction;
 9 import org.junit.jupiter.api.Test;
10 
11 /**
12  * @Author wangshuo
13  * @Date 2022/4/8, 14:50
14  * 学生控制层
15  * 一、单向多对一的映射 / 懒加载机制的介绍
16  * 二、删除
17  */
18 public class TestDXManyToOne extends CommonUtil {
19 
20     @Test
21     public void test1(){
22 
23         SessionFactory sessionFactory = getSessionFactory();
24         Session session = sessionFactory.openSession();
25         Transaction transaction = session.beginTransaction();
26 
27         //
28         HTeacherEntity hTeacherEntity = new HTeacherEntity();
29         hTeacherEntity.setTname("姚老师");
30 
31         //
32         HStudentEntity hStudentEntity = new HStudentEntity("muping",hTeacherEntity);
33         HStudentEntity hStudentEntity1 = new HStudentEntity("王硕",new HTeacherEntity("姚老师",null));
34 
35         //执行操作
36         session.save(hTeacherEntity);//先插入“一”端 的数据 这样子hibernate发起的sql语句会少一些
37         session.save(hStudentEntity);
38         session.save(hStudentEntity1);
39 
40         HStudentEntity load = session.load(HStudentEntity.class, 1);
41         //如果不使用这个load对象是不会执行查询语句的,这是hibernate的懒加载机制
42         //延申:我们使用多对一映射关系时,只查询一个表时不会发起关联对象的查询,这就是懒加载的好处。
43         //那么,如果在关联对象使用之前关闭session,就会出现懒加载异常
44         commit(transaction,sessionFactory,session);
45     }
46 
47     @Test
48     public void testDelete(){
49 
50         SessionFactory sessionFactory = getSessionFactory();
51         Session session = sessionFactory.openSession();
52         Transaction transaction = session.beginTransaction();
53 
54         //删除前也要先获取对象
55         HStudentEntity load = session.load(HStudentEntity.class, 1);
56         HTeacherEntity load1 = session.load(HTeacherEntity.class, 0);
57 
58         //可以直接删多 删一时要先把多中对应数据全部删完
59         session.delete(load);
60         //session.delete(load1);
61         commit(transaction,sessionFactory,session);
62     }
63 }

2.TestSXManyToOne

 1 package com.sdkj.hibernate.controller;
 2 
 3 import com.sdkj.hibernate.domain.HStudentEntity;
 4 import com.sdkj.hibernate.domain.HTeacherEntity;
 5 import com.sdkj.hibernate.util.CommonUtil;
 6 import org.hibernate.Session;
 7 import org.hibernate.SessionFactory;
 8 import org.hibernate.Transaction;
 9 import org.junit.jupiter.api.Test;
10 
11 import java.util.Arrays;
12 import java.util.HashSet;
13 import java.util.Set;
14 
15 /**
16  * @Author wangshuo
17  * @Date 2022/4/8, 18:48
18  * 一、单向和双向的区别:单向多对一场景只需要在多的一端查询一的一端,双向有互相查询的业务场景
19  */
20 public class TestSXManyToOne extends CommonUtil {
21 
22     @Test
23     public void test(){
24 
25         SessionFactory sessionFactory = getSessionFactory();
26         Session session = sessionFactory.openSession();
27         Transaction transaction = session.beginTransaction();
28 
29         //绕啊绕
30         HTeacherEntity hTeacherEntity = new HTeacherEntity();
31         hTeacherEntity.setTname("老师一");
32         HStudentEntity hStudentEntity = new HStudentEntity("学生1",hTeacherEntity);
33         HStudentEntity hStudentEntity1 = new HStudentEntity("学生二",hTeacherEntity);
34         HashSet<HStudentEntity> hStudentEntities = new HashSet<>();
35         hStudentEntities.add(hStudentEntity);
36         hStudentEntities.add(hStudentEntity1);
37         hTeacherEntity.setStudentEntities(hStudentEntities);
38         //绕完了 提交
39 
40         session.save(hStudentEntity);
41         session.save(hStudentEntity1);
42         session.save(hTeacherEntity);
43 
44         //测试查询排序
45         HTeacherEntity hTeacherEntity1 = session.get(HTeacherEntity.class, 2);
46         Set<HStudentEntity> studentEntities = hTeacherEntity1.getStudentEntities();
47         for (HStudentEntity studentEntity : studentEntities) {
48             System.out.println("getSid" + studentEntity.getSid());
49         }
50         commit(transaction,sessionFactory,session);
51     }
52 }