hibernate查询的6种方法。分别是
HQL查询
,对象化查询Criteria方法,动态查询DetachedCriteria,例子查询,sql查询,命名查询。
,对象化查询Criteria方法,动态查询DetachedCriteria,例子查询,sql查询,命名查询。
如果单纯的使用hibernate查询数据库只需要懂其中的一项就可以完成想要实现的一般功能,但是
从一个点,让我们掌握6中方法,则提供了更多选择。每一种方法都有其适用的情况与前提。
HQL查询
HQL是hibernate自己的一套查询语言,于SQL语法不同,具有跨数据库的优点。示例代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static void query(Stringname){
Sessions = null ;
try {
s = HibernateUtil.getSession();
// from后面是对象,不是表名
Stringhql = " fromAdminasadminwhereadmin.aname=:name " ; // 使用命名参数,推荐使用,易读。
Queryquery = s.createQuery(hql);
query.setString( " name " ,name);
List < Admin > list = query.list();
for (Adminadmin:list){
System.out.println(admin.getAname());
}
} finally {
if (s != null )
s.close();
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static void query(Stringname){
Sessions = null ;
try {
s = HibernateUtil.getSession();
// from后面是对象,不是表名
Stringhql = " fromAdminasadminwhereadmin.aname=:name " ; // 使用命名参数,推荐使用,易读。
Queryquery = s.createQuery(hql);
query.setString( " name " ,name);
List < Admin > list = query.list();
for (Adminadmin:list){
System.out.println(admin.getAname());
}
} finally {
if (s != null )
s.close();
}
}
适用情况:常用方法,比较传统,类似jdbc。缺点:新的查询语言,适用面有限,仅适用于Hibernate框架。
对象化查询Criteria方法:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static void cri(Stringname,Stringpassword){
Sessions = null ;
try {
s = HibernateUtil.getSession();
Criteriac = s.createCriteria(Admin. class );
c.add(Restrictions.eq( " aname " ,name)); // eq是等于,gt是大于,lt是小于,or是或
c.add(Restrictions.eq( " apassword " ,password));
List < Admin > list = c.list();
for (Adminadmin:list){
System.out.println(admin.getAname());
}
} finally {
if (s != null )
s.close();
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static void cri(Stringname,Stringpassword){
Sessions = null ;
try {
s = HibernateUtil.getSession();
Criteriac = s.createCriteria(Admin. class );
c.add(Restrictions.eq( " aname " ,name)); // eq是等于,gt是大于,lt是小于,or是或
c.add(Restrictions.eq( " apassword " ,password));
List < Admin > list = c.list();
for (Adminadmin:list){
System.out.println(admin.getAname());
}
} finally {
if (s != null )
s.close();
}
}
适用情况:面向对象操作,革新了以前的数据库操作方式,易读。缺点:适用面较HQL有限。
动态分离查询DetachedCriteria
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static Listdc(DetachedCriteriadc){
Sessions = HibernateUtil.getSession();
Criteriac = dc.getExecutableCriteria(s);
Listrs = c.list();
s.close();
return rs;
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static Listdc(DetachedCriteriadc){
Sessions = HibernateUtil.getSession();
Criteriac = dc.getExecutableCriteria(s);
Listrs = c.list();
s.close();
return rs;
}
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> DetachedCriteriadc = DetachedCriteria.forClass(User. class );
int id = 1 ;
if (id != 0 )
dc.add(Restrictions.eq( " id " ,id));
Dateage = new Date();
if (age != null )
dc.add(Restrictions.le( " birthday " ,age));
Listusers = dc(dc);
System.out.println( " 离线查询返回结果: " + users);
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> DetachedCriteriadc = DetachedCriteria.forClass(User. class );
int id = 1 ;
if (id != 0 )
dc.add(Restrictions.eq( " id " ,id));
Dateage = new Date();
if (age != null )
dc.add(Restrictions.le( " birthday " ,age));
Listusers = dc(dc);
System.out.println( " 离线查询返回结果: " + users);
适用情况:面向对象操作,分离业务与底层,不需要字段属性摄入到Dao实现层。 缺点:适用面较HQL有限。
例子查询
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static Listexample(Useruser){
Sessions = HibernateUtil.getSession();
List < User > users = s.createCriteria(User. class ).add(
Example.create(user)).list();
// List<User>
// users2=s.createCriteria(User.class).add((Example.create(user)).ignoreCase())
// .createCriteria("child").add((Example.create(user))).list();
return users;
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static Listexample(Useruser){
Sessions = HibernateUtil.getSession();
List < User > users = s.createCriteria(User. class ).add(
Example.create(user)).list();
// List<User>
// users2=s.createCriteria(User.class).add((Example.create(user)).ignoreCase())
// .createCriteria("child").add((Example.create(user))).list();
return users;
}
适用情况:面向对象操作。 缺点:适用面较HQL有限,不推荐。
sql查询
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static Listsql(){
Sessions = HibernateUtil.getSession();
Queryq = s.createSQLQuery( " select*fromuser " ).addEntity(User. class );
List < User > rs = q.list();
s.close();
return rs;
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static Listsql(){
Sessions = HibernateUtil.getSession();
Queryq = s.createSQLQuery( " select*fromuser " ).addEntity(User. class );
List < User > rs = q.list();
s.close();
return rs;
}
适用情况:不熟悉HQL的朋友,又不打算转数据库平台的朋友,万能方法 缺点:破坏跨平台,不易维护,不面向对象。
命名查询
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static ListnamedQuery( int id){
Sessions = HibernateUtil.getSession();
Queryq = s.getNamedQuery( " getUserById " );
q.setInteger( " id " ,id);
return q.list();
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> static ListnamedQuery( int id){
Sessions = HibernateUtil.getSession();
Queryq = s.getNamedQuery( " getUserById " );
q.setInteger( " id " ,id);
return q.list();
}
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> <? xmlversion="1.0"encoding="utf-8" ?>
<! DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping >
< class name ="com.sy.vo.User" table ="user" catalog ="news" >
![](http://www.cnblogs.com/Images/dot.gif)
![](http://www.cnblogs.com/Images/dot.gif)
</ class >
<!-- 命名查询:定义查询条件 -->
< query name ="getUserById" >
<![CDATA[ fromUserwhereid=:id ]]>
</ query >
<!-- 命名查询中使用sql,不推荐使用,影响跨数据库
<sql-queryname="getUserById2">
<![CDATA[select*fromUserwhere
]]>
</sql-query> -->
</ hibernate-mapping >
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> <? xmlversion="1.0"encoding="utf-8" ?>
<! DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping >
< class name ="com.sy.vo.User" table ="user" catalog ="news" >
![](http://www.cnblogs.com/Images/dot.gif)
![](http://www.cnblogs.com/Images/dot.gif)
</ class >
<!-- 命名查询:定义查询条件 -->
< query name ="getUserById" >
<![CDATA[ fromUserwhereid=:id ]]>
</ query >
<!-- 命名查询中使用sql,不推荐使用,影响跨数据库
<sql-queryname="getUserById2">
<![CDATA[select*fromUserwhere
![](http://www.cnblogs.com/Images/dot.gif)
</sql-query> -->
</ hibernate-mapping >