查詢按功能強弱可以分為以下幾種:
Native SQL > HQL > EJBQL > QBC > QBE
1、Native SQL
Native SQL為數據庫系統本身的SQL,里面包含了一些特有的函數等,功能也最為強大。
如:
1
@Test
2
public void testHQL_34()
{
3
Session session = sf.openSession();
4
session.beginTransaction();
5
//下面查詢使用的函數是Session的createSQLQuery
6
SQLQuery q = session.createSQLQuery("select * from category limit 2,4").addEntity(Category.class);
7
List<Category> categories = (List<Category>)q.list();
8
for(Category c : categories)
{
9
System.out.println(c.getName());
10
}
11
session.getTransaction().commit();
12
session.close();
13
14
}
2、HQL
@Test2

public void testHQL_34()
{3
Session session = sf.openSession();4
session.beginTransaction();5
//下面查詢使用的函數是Session的createSQLQuery6
SQLQuery q = session.createSQLQuery("select * from category limit 2,4").addEntity(Category.class);7
List<Category> categories = (List<Category>)q.list();8

for(Category c : categories)
{9
System.out.println(c.getName());10
}11
session.getTransaction().commit();12
session.close();13
14
}Hibernate提供的面向對象查詢語言。
如:
1
@Test
2
public void testHQL_02()
{
3
Session session = sf.openSession();
4
session.beginTransaction();
5
//Category是對象名而不是數據表名
6
Query q = session.createQuery("from Category c where c.name > 'c5'");
7
List<Category> categories = (List<Category>)q.list();
8
for(Category c : categories)
{
9
System.out.println(c.getName());
10
}
11
session.getTransaction().commit();
12
session.close();
13
14
}
3、EJBQL
@Test2

public void testHQL_02()
{3
Session session = sf.openSession();4
session.beginTransaction();5
//Category是對象名而不是數據表名6
Query q = session.createQuery("from Category c where c.name > 'c5'");7
List<Category> categories = (List<Category>)q.list();8

for(Category c : categories)
{9
System.out.println(c.getName());10
}11
session.getTransaction().commit();12
session.close();13
14
}與HQL類似,是HQL的一個子集
4、QBC
Query By Criteria,即帶約束/條件的查詢
如:
1
@Test
2
public void testQBC()
{
3
Session session = sf.openSession();
4
session.beginTransaction();
5
//criterion 標準/準則/約束
6
Criteria c = session.createCriteria(Topic.class) //from Topic
7
8
.add(Restrictions.gt("id", 2)) //greater than = id > 2
9
.add(Restrictions.lt("id", 8)) //little than = id < 8
10
.add(Restrictions.like("title", "t_"))
11
.createCriteria("category")
12
.add(Restrictions.between("id", 3, 5)) //category.id >= 3 and category.id <=5
13
;
14
15
//DetachedCriterea
16
for(Object o : c.list())
{
17
Topic t = (Topic)o;
18
System.out.println(t.getId() + "-" + t.getTitle());
19
}
20
session.getTransaction().commit();
21
session.close();
22
23
}
5、QBE
@Test2

public void testQBC()
{3
Session session = sf.openSession();4
session.beginTransaction();5
//criterion 標準/準則/約束6
Criteria c = session.createCriteria(Topic.class) //from Topic7
8
.add(Restrictions.gt("id", 2)) //greater than = id > 29
.add(Restrictions.lt("id", 8)) //little than = id < 810
.add(Restrictions.like("title", "t_"))11
.createCriteria("category")12
.add(Restrictions.between("id", 3, 5)) //category.id >= 3 and category.id <=513
;14

15
//DetachedCriterea16

for(Object o : c.list())
{17
Topic t = (Topic)o;18
System.out.println(t.getId() + "-" + t.getTitle());19
}20
session.getTransaction().commit();21
session.close();22
23
}Query By Example
如:
1
@Test
2
public void testQBE()
{
3
Session session = sf.openSession();
4
session.beginTransaction();
5
Topic tExample = new Topic();
6
tExample.setTitle("T_");
7
8
//創建一個例子對象,然后設置其相應的屬性
9
//QBE僅適合于給特定值的查詢
10
Example e = Example.create(tExample)
11
.ignoreCase().enableLike();
12
//QBC除了添加自己的條件,最后將例子對象e也當成條件添加進來
13
//QBC可以添加給特定值的條件,也可添加給了一定范圍的條件
14
Criteria c = session.createCriteria(Topic.class)
15
.add(Restrictions.gt("id", 2))
16
.add(Restrictions.lt("id", 8))
17
.add(e)
18
;
19
//from Topic t where t.id>2 and t.id<8 and t.title like 'T_'
20
21
22
for(Object o : c.list())
{
23
Topic t = (Topic)o;
24
System.out.println(t.getId() + "-" + t.getTitle());
25
}
26
session.getTransaction().commit();
27
session.close();
28
29
}
@Test2

public void testQBE()
{3
Session session = sf.openSession();4
session.beginTransaction();5
Topic tExample = new Topic();6
tExample.setTitle("T_");7
8
//創建一個例子對象,然后設置其相應的屬性9
//QBE僅適合于給特定值的查詢10
Example e = Example.create(tExample)11
.ignoreCase().enableLike();12
//QBC除了添加自己的條件,最后將例子對象e也當成條件添加進來13
//QBC可以添加給特定值的條件,也可添加給了一定范圍的條件14
Criteria c = session.createCriteria(Topic.class)15
.add(Restrictions.gt("id", 2))16
.add(Restrictions.lt("id", 8))17
.add(e)18
;19
//from Topic t where t.id>2 and t.id<8 and t.title like 'T_'20
21
22

for(Object o : c.list())
{23
Topic t = (Topic)o;24
System.out.println(t.getId() + "-" + t.getTitle());25
}26
session.getTransaction().commit();27
session.close();28
29
}以上幾種方法,其中QBC、QBE更加符合面向對象編程(僅此而已)。

