As a veteran ORM framework, Hibernate's contribution to the database persistence layer is a visible achievement.
It provides more and more data query methods, from SQL to self-created HQL to object-oriented standardized queries.
Although the query is a bit confusing, the configuration is a bit more complicated to use.
But there's no way to hide its fascinating place, this blog tries to summarize all of Hibernate's queries.
Radish green vegetables each their own, work can choose according to their own preferences of several of them to program.
1. HQL mode, parameter use question mark placeholder (obsolete in 4.1 version)
Public list<userpo> getuserlist (Userpo userpo) { String hql = ' from Userpo where name =? and passwd=? "; Query query = gethibernatesession (). CreateQuery (HQL); Query.setparameter (0, Userpo.getname ()); Query.setparameter (1, userpo.getpasswd ()); return Query.list (); }
2. HQL mode, parameter use named placeholder
Public list<userpo> getuserlist (Userpo userpo) { String hql = ' from Userpo where name =: UserName and passwd=: U Serpwd "; Query query = getsession (). CreateQuery (HQL); Query.setparameter ("UserName", Userpo.getname ()); Query.setparameter ("Userpwd", userpo.getpasswd ()); return Query.list (); }
3. HQL mode, parameters using JPA placeholders
Public list<userpo> getuserlist (Userpo userpo) { String hql = "from Userpo where name =? 1 and passwd=? 2"; Query query = getsession (). CreateQuery (HQL); Query.setparameter ("1", Userpo.getname ()); Query.setparameter ("2", userpo.getpasswd ()); return Query.list (); }
4. HQL mode, parameters using object binding
Public list<userpo> getuserlist (Userpo userpo) { String hql = ' from Userpo where name =: Name and passwd=:p WD "; Query query = getsession (). CreateQuery (HQL); Query.setproperties (USERPO); return Query.list (); }
PS: It is important to note that the named parameter placeholder is consistent with the PO attribute, and can only be used to name the placeholder in the form of a parameter.
Of course, the above 1--4 fill Query method, HQL can be adjusted to SQL, is interlinked.
5. QBC (Query by Criteria) method, parameters using Restrictions object
Public list<userpo> getuserlist (Userpo userpo) { criteria = getsession (). Createcriteria ( Userpo.class); Criteria.add (Restrictions.eq ("name", Userpo.getname ())); Criteria.add (Restrictions.eq ("passwd", USERPO.GETPASSWD ())); return Criteria.list (); }
6. QBC (Query by Criteria) method, parameters use object binding
Public list<userpo> getuserlist (Userpo userpo) { criteria = getsession (). Createcriteria ( Userpo.class); Criteria.add (Example.create (Userpo)); return Criteria.list (); }
The key abstract objects used by the PS:QBC (Query by Criteria) are:
Restrictions--set query throttling conditions
Order--Set query sort criteria
Projections---the tools class methods are counted and grouped.
The above 5-6 is also my favorite way to use object-oriented query to cater to object-oriented programming.
7. Offline Conditional Query
Public list<userpo> getuserlist (Userpo userpo) { Detachedcriteria Detachedcriteria = Detachedcriteria.forclass (userpo.class); Detachedcriteria.add (Restrictions.eq ("name", Userpo.getname ())); Detachedcriteria.add (Restrictions.eq ("passwd", USERPO.GETPASSWD ())); Criteria = Detachedcriteria.getexecutablecriteria (getsession ()); return Criteria.list (); }
The benefits of offline querying, you can pass query object Detachedcriteria as a parameter to the DAO layer, reducing the DAO layer code
8. QBC (query by Criteria) method, paging inquiry
Public list<userpo> getuserlist (Userpo userpo) { criteria = getsession (). Createcriteria ( Userpo.class); Criteria.setfirstresult (4); Criteria.setmaxresults (3); return Criteria.list (); }}
PS: Paging query is mainly to specify two parameters (starting from what, how many bars):
Setfirstresult () and Setmaxresults () of the query or Criteria object
Of course, the Criteria object in 8 can also be swapped for the query object, using HQL or SQL queries.
Hibernate Query Method (HQL/QBC/QBE) Summary