We learn hibernate query is to know HQL support condition query, divided into many aspects, here we have a specific hibernate query with examples to explain, hope to help the reader's study.
1. Support string method parameter pass query:
Example:
List students = session.createQuery("select s.id,s.name from Student s where s.name like '%1' ").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] o = (Object[])iter.next();
System.out.println(o[0]+","+o[1]);
}
Because there are multiple property queries involved, the object array type is returned.
2, support similar to the preparestatement way? Pass parameter query
Example:
List students = session.createQuery("select s.id,s.name from Student s where s.name like :goodname ")
.setParameter("goodname", "%1%").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] o = (Object[])iter.next();
System.out.println(o[0]+","+o[1]);
}
To set the parameter starting at 0, see Line 2nd.
3, go variable mode parameter transfer
Example:
List students = session.createQuery("select s.id,s.name from Student s where s.name like :goodname ")
.setParameter("goodname", "%1%").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] o = (Object[])iter.next();
System.out.println(o[0]+","+o[1]);
}
This is similar to the one in 2, except that many parameters can be grouped into a class.
4. Support multi-parameter transmission
Example:
List students = session.createQuery("select s.id, s.name from Student s where s.id in(:myids)")
.setParameterList("myids", new Object[]{1, 2, 3, 4, 5,32,13,14})
.list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] o = (Object[])iter.next();
System.out.println(o[0]+","+o[1]);
}
Note that the calling method is Setparameterlist.