Hibernate进阶学习4

Hibernate进阶学习4

深入学习hibernate的查询语句

 

测试HQL查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.hibernate.test;
 
import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
 
import java.util.List;
 
/**
 * @author: XDZY
 * @date: 2018/11/16 10:26
 * @description: 测试HQL语句(hibernate独有的面向对象的语法)(适合不复杂的多表查询)
 */
public class HibernateTest {
    /**
     * 排序查询
     */
    @Test
    public void test1() {
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();
 
        /********************* 数据库操作 **********************/
 
        //1)书写HQL语句
        String hql = "from Customer order by cust_id";
        //2)创建查询对象
        Query query = session.createQuery(hql);
        //3)执行查询
        List<Customer> list = query.list();
 
        System.out.println(list);
 
        /*******************************************************/
 
        tx.commit();
        session.close();
    }
 
    /**
     * 统计查询
     */
    @Test
    public void test2() {
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();
 
        /********************* 数据库操作 **********************/
 
        //1)书写HQL语句
        String hql = "select count(*) from Customer";
        String hql1 = "select sum(cust_id) from Customer";
        String hql2 = "select avg(cust_id) from Customer";
        String hql3 = "select max(cust_id) from Customer";
        String hql4 = "select min(cust_id) from Customer";
        //2)创建查询对象
        Query query = session.createQuery(hql2);
        //3)执行查询
        Number number = (Number) query.uniqueResult();
 
        System.out.println(number);
 
        /*******************************************************/
 
        tx.commit();
        session.close();
    }
 
    /**
     * 投影查询
     */
    @Test
    public void test3() {
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();
 
        /********************* 数据库操作 **********************/
 
        //1)书写HQL语句
        String hql = "select new Customer(cust_id,cust_name) from Customer";
        //2)创建查询对象
        Query query = session.createQuery(hql);
        //3)执行查询
        List<Customer> list = query.list();
 
        System.out.println(list);
 
        /*******************************************************/
 
        tx.commit();
        session.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.hibernate.test;
 
import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
 
import java.util.Arrays;
import java.util.List;
 
/**
 * @author: XDZY
 * @date: 2018/11/19 14:35
 * @description: 测试HQL多表查询(hibernate独有的面向对象的语法)(适合不复杂的多表查询)
 */
public class HibernateTest2 {
    /**
     * 内连接
     */
    @Test
    public void test1() {
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();
 
        /********************* 数据库操作 **********************/
 
        //1)书写HQL语句
        String hql = "from Customer c inner join c.linkMens";
        //2)创建查询对象
        Query query = session.createQuery(hql);
        //3)执行查询
        List<Object[]> list = query.list();
 
        for (Object[] obj : list) {
            System.out.println(Arrays.toString(obj));
        }
 
        /*******************************************************/
 
        tx.commit();
        session.close();
    }
 
    /**
     * 迫切内连接
     * 将查询到的关联的对象也封装到查询的对象中
     */
    @Test
    public void test2() {
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();
 
        /********************* 数据库操作 **********************/
 
        //1)书写HQL语句
        String hql = "from Customer c inner join fetch c.linkMens";
        //2)创建查询对象
        Query query = session.createQuery(hql);
        //3)执行查询
        List<Customer> list = query.list();
 
        System.out.println(list);
 
        /*******************************************************/
 
        tx.commit();
        session.close();
    }
 
    /**
     * 左(右)外连接
     */
    @Test
    public void test3() {
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();
 
        /********************* 数据库操作 **********************/
 
        //1)书写HQL语句
        //String hql="from Customer c left join c.linkMens";
        String hql = "from Customer c right join c.linkMens";
        //2)创建查询对象
        Query query = session.createQuery(hql);
        //3)执行查询
        List<Object[]> list = query.list();
 
        for (Object[] obj : list) {
            System.out.println(Arrays.toString(obj));
        }
 
        /*******************************************************/
 
        tx.commit();
        session.close();
    }
}

 

测试Criteria查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.hibernate.test;
 
import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.*;
import org.junit.Test;
 
import java.util.List;
 
/**
 * @author: XDZY
 * @date: 2018/11/16 10:26
 * @description: 测试criteria语句(hibernate独有的无语句的全面向对象的查询语法)(适合单表查询)
 */
public class HibernateTest3 {
    /**
     * 排序查询
     */
    @Test
    public void test() {
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();
 
        /********************* 数据库操作 **********************/
 
        Criteria criteria = session.createCriteria(Customer.class);
        //criteria.addOrder(Order.asc("cust_id"));
        criteria.addOrder(Order.desc("cust_id"));
        List list = criteria.list();
        System.out.println(list);
 
        /*******************************************************/
 
        tx.commit();
        session.close();
    }
 
    /**
     * 离线查询
     * 就是在不创建session的情况下也能进行数据库操作(比如在service和web层调用)
     */
    @Test
    public void test1() {
        //创建离线对象
        DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
        dc.add(Restrictions.idEq(3L));
 
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();
 
        /********************* 数据库操作 **********************/
 
        Criteria criteria = dc.getExecutableCriteria(session);
        List list = criteria.list();
        System.out.println(list);
 
        /*******************************************************/
 
        tx.commit();
        session.close();
    }
}

 

测试类级别加载策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.domain">
    <class name="Customer" table="cst_customer" lazy="false">
        <id name="cust_id">
            <generator class="native"></generator>
        </id>
        <property name="cust_name" column="cust_name"></property>
        <property name="cust_source" column="cust_source"></property>
        <property name="cust_industry" column="cust_industry"></property>
        <property name="cust_level" column="cust_level"></property>
        <property name="cust_linkman" column="cust_linkman"></property>
        <property name="cust_phone" column="cust_phone"></property>
        <property name="cust_mobile" column="cust_mobile"></property>
 
        <!-- lazy属性:决定是否延迟加载
                true(默认值):延迟加载,懒加载
                false:立即加载
                extra:极其懒惰
             fetch属性:决定加载策略,使用什么类型的sql语句加载集合数据
                select(默认值):单表查询加载
                join:使用多表查询加载集合
                subselect:使用子查询加载集合 -->
        <!-- batch-size:抓取集合的数量为3
                抓取客户的集合时,一次抓取几个客户的联系人集合 -->
        <set name="linkMens" batch-size="3">
            <key column="lkm_cust_id"></key>
            <one-to-many class="LinkMan"/>
        </set>
    </class>
</hibernate-mapping>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.domain">
    <class name="LinkMan" table="cst_linkman">
        <id name="lkm_id">
            <generator class="native"></generator>
        </id>
        <property name="lkm_gender"></property>
        <property name="lkm_name"></property>
        <property name="lkm_phone"></property>
        <property name="lkm_email"></property>
        <property name="lkm_qq"></property>
        <property name="lkm_mobile"></property>
        <property name="lkm_memo"></property>
        <property name="lkm_position"></property>
 
        <!-- fetch属性:决定加载的sql语句
                select:使用单表查询
                join:多表查询
             lazy属性:决定加载时机
                false:立即加载
                proxy:由customer的类级别加载策略决定 -->
        <many-to-one name="customer" column="lkm_cust_id" class="Customer" fetch="join" lazy="proxy">
        </many-to-one>
    </class>
</hibernate-mapping>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.hibernate.test;
 
import com.hibernate.domain.Customer;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.*;
import org.junit.Test;
 
/**
 * @author: XDZY
 * @date: 2018/11/16 10:26
 * @description: 类级别加载策略
 */
public class HibernateTest4 {
    /**
     * 懒加载|延迟加载
     */
    @Test
    public void test() {
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();
 
        /********************* 数据库操作 **********************/
 
        //立即加载
        //Customer customer = session.get(Customer.class, "2L");
 
        //延迟加载:查询时只返回一个代理对象,在使用时,根据关联的session查询数据库返回结果
        //为了更好的性能,建议延迟加载;延迟加载貌似只是推迟了查询
        Customer customer = session.load(Customer.class, "2L");
        System.out.println(customer);
 
        /*******************************************************/
 
        tx.commit();
        session.close();
    }
}

 

测试关联级别加载策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.hibernate.test;
 
import com.hibernate.domain.Customer;
import com.hibernate.domain.LinkMan;
import com.hibernate.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
 
import java.util.Set;
 
/**
 * @author: XDZY
 * @date: 2018/11/16 10:26
 * @description: 关联级别加载策略
 */
public class HibernateTest5 {
    /**
     * lazy与fetch的使用
     */
    @Test
    public void test() {
        //创建Session对象
        Session session = HibernateUtils.openSession();
        //开启事务并获取事务对象
        Transaction tx = session.beginTransaction();
 
        /********************* 数据库操作 **********************/
 
        //立即加载
        Customer customer = session.get(Customer.class, "2L");
 
        Set<LinkMan> linkMens = customer.getLinkMens();
        System.out.println(linkMens.size());
        System.out.println(linkMens);
 
        /*******************************************************/
 
        tx.commit();
        session.close();
    }
}

 

posted @   肖德子裕  阅读(150)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示