Hibernate Query Method (hql/qbc/qbe) Summary

Source: Internet
Author: User

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) {        = "from Userpo where name =?" and passwd=? " ;         = 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) {        = "from userpo where name =: userName and passwd=: userpwd" 
    
     ;         =
      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) {        = "from userpo where name =? 1 and passwd=? 2";        = 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) {        = "from userpo where name =: name and Passwd=:p asswd"; 
     = 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) {        = 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) {        = 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.forclass (userpo.  Class);        Detachedcriteria.add (restrictions.eq ("name", Userpo.getname ()));        Detachedcriteria.add (restrictions.eq ("passwd", userpo.getpasswd ()));         = 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) {        = 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.