一、检索策略概述

 

 

 

 

 

 

 

 

 

 

 

 二、initialize()

 1 package com.sdkj.hibernate.controller;
 2 
 3 import com.sdkj.hibernate.domain.HTeacherEntity;
 4 import com.sdkj.hibernate.util.CommonUtil;
 5 import org.hibernate.Hibernate;
 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/9, 16:59
14  * Please add a comment
15  */
16 public class TestInitialize extends CommonUtil {
17 
18     @Test
19     public void test(){
20 
21         SessionFactory sessionFactory = getSessionFactory();
22         Session session = sessionFactory.openSession();
23         Transaction transaction = session.beginTransaction();
24 
25         HTeacherEntity entity = session.load(HTeacherEntity.class, 3);
26         //hibernate提供的立即查询的方法,可以在懒加载机制下直接查询数据库
27         Hibernate.initialize(entity);
28 
29         commit(transaction,sessionFactory,session);
30     }
31 }

三、多对一或多对多集合属性配置

 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" fetch="join" inverse="true" batch-size="10" order-by="sid asc">
15             <!-- studentEntities对应实体类get /
16             fetch = {
17                 select 默认查询
18                 subselect 子查询(此时batch-size失效)
19                 join 连接查询(默认) 建议使用此查询方式(此时batch-size失效)
20             }
21             inverse = true 设置不维护外键关系,设置此项可减少冗余的访问数据库次数 /
22             batch-size = 10 设置一次查询找多少个对应set
23             order by = tid asc 设置拼接在sql语句最后的排序  -->
24             <key>
25                 <column name="tid"/>
26             </key>
27             <one-to-many class="com.sdkj.hibernate.domain.HStudentEntity"/>
28         </set>
29     </class>
30 </hibernate-mapping>

五、hibernate的几种对象检索方式

 

 六、HQL检索方式

 

 

 

 七、简单test类

 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.hibernate.query.NativeQuery;
10 import org.hibernate.query.Query;
11 import org.junit.jupiter.api.Test;
12 
13 import java.util.Arrays;
14 import java.util.List;
15 
16 /**
17  * @Author wangshuo
18  * @Date 2022/4/9, 17:14
19  * Please add a comment
20  */
21 public class TestHql extends CommonUtil {
22 
23     @Test
24     public void testHql_One(){
25 
26         SessionFactory sessionFactory = getSessionFactory();
27         Session session = sessionFactory.openSession();
28         Transaction transaction = session.beginTransaction();
29 
30         String hql = " from HStudentEntity ";
31         Query<HStudentEntity> query = session.createQuery(hql);
32         List<HStudentEntity> list = query.list();
33         for (HStudentEntity hTeacherEntity : list) {
34             System.out.println(hTeacherEntity.toString());
35         }
36 
37         commit(transaction,sessionFactory,session);
38     }
39 
40     @Test
41     public void test_Two(){
42 
43         SessionFactory sessionFactory = getSessionFactory();
44         Session session = sessionFactory.openSession();
45         Transaction transaction = session.beginTransaction();
46         //写hql
47         String hql = "from HTeacherEntity";
48         //session 获取 query
49         Query<HTeacherEntity> query = session.createQuery(hql);
50         //添加查询条件
51         //执行hql语句
52         for (HTeacherEntity hTeacherEntity : query.list()) {
53 
54             System.out.println(hTeacherEntity);
55         }
56 
57         //hibernate执行sql查询
58         String sql = "select * from h_teacher where tid =:tid and tname like :tname";
59         NativeQuery<Object[]> tid = session.createNativeQuery(sql).setParameter("tid", 2).setParameter("tname","%老%");
60         for (Object[] hTeacherEntity : tid.list()) {
61 
62             //数组不要直接写toString() 要写Arrays.toString(arr)
63             System.out.println(Arrays.toString(hTeacherEntity));
64         }
65         commit(transaction,sessionFactory,session);
66     }
67 }