Hibernate查询_HQL_EJBQL_QBC_QBE
2011-05-12 09:49
357 查看
查询按功能强弱可以分为以下几种:
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
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
与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
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
}
以上几种方法,其中QBC、QBE更加符合面向对象编程(仅此而已)。
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
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
与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
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
}
以上几种方法,其中QBC、QBE更加符合面向对象编程(仅此而已)。
相关文章推荐
- Hibernate查询_HQL_EJBQL_QBC_QBE
- Hibernate查询(Query Language,hql,ejbql,nativeql,qbc,qbe)
- Hibernate查询_HQL_EJBQL_QBC_QBE
- hibernate查询精典例子(HQL QBC QBE)
- Hibernate的几种查询方式-HQL,QBC,QBE,离线查询,复合查询,分页查询
- Hibernate 查询方式(HQL/QBC/QBE)汇总
- Hibernate 查询方式(HQL/QBC/QBE)汇总
- Hibernate通常是三种:hql查询,QBC查询和QBE查询:
- Hibernate查询_HQL_EJBQL_QBC_QBE…
- Hibernate的几种查询方式-HQL,QBC,QBE,离线查询,复合查询,分页查询
- Hibernate 查询方式(HQL/QBC/QBE)汇总
- Hibernate通常是三种:hql查询,QBC查询和QBE查询:
- Hibernate 查询方式(HQL/QBC/QBE)汇总
- Hibernate通常是三种:hql查询,QBC查询和QBE查询:
- Hibernate的几种查询方式 HQL,QBC,QBE,离线查询,复合查询,分页查询
- Hibernate的几种查询方式-HQL,QBC,QBE,离线查询,复合查询,分页查询
- Hibernate中的三种查询语句(hql,sql,qbc)
- 小谈Hibernate中的HQL与QBC查询
- Hibernate学习笔记 -- day07 HQL、QBC查询用法
- Hibernate的集中查询方式 : hql查询,QBC查询和QBE查询