1. Find (String hql); General Query
Example: This.gethibernatetemplate (). Find ("from User");
2. Find (String hql,object value);//A query condition
Example: This.gethibernatetemplate (). Find ("from User u where u.name=?", "Test");
3. Find (String hql,object[] values);//Multiple query conditions
Example: This.gethibernatetemplate (). Find ("from User u where u.name=?"). And u.pwd=? ", New string[]{" test "," 123 "});
4. Findbyexample (Object exampleentity,int firstresult, int maxResults)//paging use
Example:
User user= new User (); U.setactive ("Active");
List list=this.gethibernatetemplate (). Findbyexample (User,firstresult,maxresults);
Query result: User with status Active (object count from 0 to 20)
5. Findbynamedparam (String hql,string paramname,object value); A query condition
Example:
Hql= "from User u where u.name=:p arname";
Paramname= "Parname";
Value= "BB"
List List = This.getHibernateTemplate.findByNamedParam (Hql,paramname,value);
Query result: Users with a name of BB
6. Findbynamedparam (String queryString, string[] paramname, object[] value)//Multiple query conditions
Example:
Hql= "from User u where u.name=:myname and u.pwd =:mypwd";
String[] paramname= new string[]{"MyName", "MyPwd"};
Sring[] value=new strign[]{"BB", "123"};
List List = This.getHibernateTemplate.findByNamedParam (Hql,paramname,value);
Query Result: A user named BB password is 123
7. Pagination HQL Example
Public List excutehqlpage (final String hqlstr, final int startrow,final int rowCount) throws Daoexception {
List<object[]> list;
try {
List = Gethibernatetemplate (). Executefind (New Hibernatecallback () {
Public Object Doinhibernate (session session)
Throws Hibernateexception, SQLException {
Org.hibernate.Query Query = (org.hibernate.Query) session.createquery (HQLSTR);
Query.setfirstresult (StartRow);//define the query from the first few
Query.setmaxresults (RowCount);//define the number of records returned
List List = Query.list ();
return list;
}
});
} catch (Exception e) {
throw new Daoexception (DAOEXCEPTION.ERRORCODE_EXCUTEHQL);
}
return list;
}
8. Query according to Hql/sql
Public List querybyhql (final String hql, final object[] prams,final String sql) {
Return (List) gethibernatetemplate (). Execute (new Hibernatecallback () {
Public Object Doinhibernate (session session)
Throws Hibernateexception, SQLException {
if (Hql!=null && hql.length () >0) {
Query query=session.createquery (HQL);
if (prams!=null && prams.length>0) {
for (int i=0;i<prams.length;i++) {
Query.setparameter (I,prams[i]);
}
}
return Query.list ();
}else{
SQLQuery sqlquery=session.createsqlquery (SQL);
return Sqlquery.list ();
}
}
});
}
9. Save/Update
Public String saveorupdateobject (Isupervo vo) throws daoexception {
try {
String id = null;
if (Stringutil.isempty (Vo.getpid ())) {
Gethibernatetemplate (). Save (VO);
} else {
Gethibernatetemplate (). Merge (VO);
}
id = vo.getpid ();
return ID;
} catch (Exception e) {
E.printstacktrace ();
}
}
Gethibernatetemplate (). Delete (VO); Delete
11. Delete by condition
Public Integer Deleteobjectsbywherepart (final Class voclass,final String Wherepart, final object[] parmaters) throws daoexception {
try {
Integer count = (integer) gethibernatetemplate (). Execute (new Hibernatecallback () {
Public Object Doinhibernate (session session) throws Hibernateexception, SQLException {
Integer coun = null;
String hql = "Delete from" + voclass.getname () + "where 1=1";
if (Wherepart! = null && Wherepart.trim (). Length () > 0) {
HQL = hql + "and" + Wherepart;
}
Query query = session.createquery (HQL);
Object obj = null;
if (parmaters! = null && parmaters.length > 0) {
for (int i = 0; i < parmaters.length; i++) {
obj = Parmaters[i];
Query.setparameter (i, obj);
}
}
Coun = Query.executeupdate ();
Return coun;
}
});
return count;
} catch (Exception e) {
E.printstacktrace ();
}
}
HQL Hibernate.gethibernatetemplate ()