Hibernate4Step7

hardwork

本着应用的目的快速学习Hibernate4框架

  • Hibernate查询

前面的话

本节主要介绍:

本节主要讲解Hibernate4的查询方式,主要包括如下:

  1. 导航对象图查询方式
  2. OID查询方式
  3. 本地SQL查询方式
  4. HQL查询方式
  5. QBC(Query By Criteria)查询方式

一、导航对象图查询方式

比如说学生和班级有多对一关系,在查询学生的时候,通过学生端把所有的班级查询出来。

二、OID查询方式

比如说get()/load()方法获取数据。

三、本地SQL查询方式

比如说用的MySQL数据库,那么就根据MySQL的SQL语法进行查询。

如果说用的Oracle数据库,那么就用的Oracle语法进行查询。

本地SQL查询方式的一个缺陷就是不能跨数据库,如果换了个数据库,可能就要重新编写新的SQL代码

四、HQL查询方式

HQL(Hibernate Query Language)是面向对象的查询语言;是使用最广的查询方式。

五、QBC查询方式

QBC(Query By Criteria)是Hibernate提供的一套用于查询的接口,可以根据这套接口进行查询。

JUnitTest

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package cn.codeforgod.test;


import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import cn.codeforgod.model.Student;
import cn.codeforgod.util.HibernateUtil;

public class StudentTest
{

private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

private Session session;

private Transaction transaction;

@Before
public void setUp() throws Exception
{

session = sessionFactory.openSession();
transaction = session.beginTransaction();
}

@After
public void tearDown() throws Exception
{

transaction.commit();
session.close();
sessionFactory.close();
}

// 使用本地化SQL进行普通查询
@Test
public void testSQLQuery1() throws Exception
{

String sql = "select * from t_student";
List<Student> studentList = (List<Student>) session.createSQLQuery(sql).addEntity(Student.class).list();
for (Student student : studentList)
{
System.out.println(student);
}
}

// 本地化SQL进行带条件的查询
@Test
public void testSQLQuery2() throws Exception
{

String sql = "select * from t_student where studentName like :studentName and studentAge = :studentAge";
Query query = session.createSQLQuery(sql).addEntity(Student.class);
query.setString("studentName", "张%");
query.setInteger("studentAge", 14);
List<Student> studentList = query.list();
for (Student student : studentList)
{
System.out.println(student);
}
}

// 使用HQL进行普通查询
@Test
public void testHQLQuery1() throws Exception
{

String hql = "from Student";
Query query = session.createQuery(hql);
List<Student> studentList = query.list();
for (Student student : studentList)
{
System.out.println(student);
}
}

// 使用HQL进行带条件的查询
@Test
public void testHQLQuery2() throws Exception
{

String hql = "from Student where name like :studentName and age = :studentAge";
Query query = session.createQuery(hql);
query.setString("studentName", "张%");
query.setInteger("studentAge", 14);
List<Student> studentList = query.list();
for (Student student : studentList)
{
System.out.println(student);
}
}

// 使用HQL进行带条件的查询
@Test
public void testHQLQuery3() throws Exception
{

String hql = "from Student where name like :studentName and age = :studentAge";
Query query = session.createQuery(hql);
query.setString("studentName", "张%");
query.setInteger("studentAge", 14);
List<Student> studentList = query.list();
for (Student student : studentList)
{
System.out.println(student);
}
}

// 使用HQL进行创建别名的查询
@Test
public void testHQLQuery4() throws Exception
{

String hql = "from Student s where s.name like :studentName and s.age = :studentAge";
Query query = session.createQuery(hql);
query.setString("studentName", "张%");
query.setInteger("studentAge", 14);
List<Student> studentList = query.list();
for (Student student : studentList)
{
System.out.println(student);
}
}

// 使用HQL进行普通查询并对结果以按照age降序排序
@Test
public void testHQLQuery5() throws Exception
{

String hql = "from Student order by age desc";
Query query = session.createQuery(hql);
List<Student> studentList = query.list();
for (Student student : studentList)
{
System.out.println(student);
}
}

// 使用HQL进行分页查询
@Test
public void testHQLQuery6() throws Exception
{

String hql = "from Student";
Query query = session.createQuery(hql);
query.setFirstResult(0); // 从何开始 limit 0,2
query.setMaxResults(2); // 共有几条
List<Student> studentList = query.list();
for (Student student : studentList)
{
System.out.println(student);
}
}

// 使用HQL查询单个对象
@Test
public void testHQLQuery7() throws Exception
{

String hql = "from Student";
Query query = session.createQuery(hql);
query.setFirstResult(2);
query.setMaxResults(1);
Student student = (Student) query.uniqueResult();
System.out.println(student);
}

// 使用QBC进行普通查询
@Test
public void testQBCQuery1() throws Exception
{

Criteria criteria = session.createCriteria(Student.class);
List<Student> studentList = criteria.list();
for (Student student : studentList)
{
System.out.println(student);
}
}

// 使用QBC进行带条件查询&单个结果
@Test
public void testQBCQuery2() throws Exception
{

Criteria criteria = session.createCriteria(Student.class);
Criterion c1 = Restrictions.like("name", "张%");
Criterion c2 = Restrictions.eq("age", 14);
criteria.add(c1);
criteria.add(c2);
Student student = (Student) criteria.uniqueResult();
System.out.println(student);
}

// 使用QBC进行普通查询并对结果以按照age降序排序
@Test
public void testQBCQuery3() throws Exception
{

Criteria criteria = session.createCriteria(Student.class);
criteria.addOrder(Order.desc("age"));
List<Student> studentList = criteria.list();
for (Student student : studentList)
{
System.out.println(student);
}
}

// 使用QBC进行分页查询
@Test
public void testQBCQuery4() throws Exception
{

Criteria criteria = session.createCriteria(Student.class);
criteria.setFirstResult(0);
criteria.setMaxResults(2);
List<Student> studentList = criteria.list();
for (Student student : studentList)
{
System.out.println(student);
}
}
}
坚持原创技术分享,您的支持将鼓励我继续创作