Hibernate several query ways-HQL,QBC,QBE, offline query, compound query, paged query

Source: Internet
Author: User

Hql Query method

This is one of my most used and favorite, because it is flexible and intuitive to write, and not much worse than the syntax of the familiar SQL. Conditional query, paged query, connection query, nested query, write up and SQL syntax basically consistent, the only difference is to replace the table name with the class or object. Other, including some query functions (count (), sum (), etc.), query conditions, and so on, all the same as SQL syntax.

Example:
Session session = Sessionfactory.getcurrentsession (); User user = null; Transaction ts = session.begintransaction (); try {     query query = Session.createquery ("From User as U where name= ' Ijse '");     User= (User) query.list (). get (0);     Session.commit (); } catch (Hibernateexception ex) {     ts.rollback ();     Ex.printstacktrace (); } System.out.println (User.getname ());

  

QBC (query by Criteria) Inquiry method

This approach compares the object-oriented approach, with the emphasis on three objects that describe the condition: restrictions,order,projections. The following three steps are generally required to use QBC queries:

    1. Create a criteria object using the Createcriteria () method of the session instance
    2. Use the tool class restrictions method to set the query criteria for the criteria object, the Order tool class methods to set the sorting method, projections the method of the tool class to be counted and grouped.
    3. Use the list () method of the criteria object to query and return results

Common methods of the restrictions class:

Method name Describe
Restrictions.eq Equals
Restrictions.alleq Use Map,key/valu for multiple equals
Restrictions.gt Greater than
Restrictions.ge Greater than or equal
restrictions.lt Less than
Restrictions.le Less than or equal
Restrictions.between The between of the corresponding SQL
Restrictions.like The like of the corresponding SQL
Restrictions.in The in of the corresponding SQL
Restrictions.and and relationship
Restrictions.or or relationship
Restrictions.sqlrestriction SQL qualified Query

Common methods for order classes:

Method name Describe
Order.asc Ascending
Order.desc Descending

Common methods of projections class

Method name Describe
Projections.avg Averaging
Projections.count Count the number of a property
Projections.countdistinct Count the number of different values for a property
Projections.groupproperty Specifies that a property is a grouped property
Projections.max To find the maximum value
Projections.min To find the minimum value
Projections.projectionlist Create a Projectionlist object
Projections.rowcount Number of record bars in the query result set
Projections.sum To find the total of a property

Example:
Session session = Sessionfactory.getcurrentsession (); User user = null; Transaction ts = session.begintransaction (); try {     criteria = Session.createcriteria (user.class);     Criteria.add (Restrictions.eq ("name", "Ijse"));     User= (User) criteria.list (). get (0);         Session.commit (); } catch (Hibernateexception ex) {     ts.rollback ();     Ex.printstacktrace (); } System.out.println (User.getname ());

  

QBE (query by Example) Query method

Queries a non-empty property of an object as a query condition.

Example:
Session session = Sessionfactory.getcurrentsession (); User user = new user (); User.setname ("Ijse"); Transaction ts = session.begintransaction (); try {     criteria = Session.createcriteria (user.class);     Criteria.add (example.create (user));     User= (User) criteria.list (). get (0);         Session.commit (); } catch (Hibernateexception ex) {     ts.rollback ();     Ex.printstacktrace (); } System.out.println (User.getname ());

  

Offline query

Offline query is to set up a Detachedcriteria object, specify the condition of the query, and then pass the object in after Session.begintransaction (). Typically, this object can be established in the presentation layer and then passed into the business layer for querying.

Example:
Detachedcriteria Detachedcriteria = Detachedcriteria.forclass (User.class); Detachedcriteria.add (Restrictions.eq ("name", "Ijse");   Session session = Sessionfactory.getcurrentsession (); User user = new user (); Transaction ts = session.begintransaction (); try {     criteria = Detachedcriteria.getexecutablecriteria (session);     User= (User) criteria.list (). get (0);         Session.commit (); } catch (Hibernateexception ex) {     ts.rollback ();     Ex.printstacktrace (); } System.out.println (User.getname ());

  

Compound query

Compound query is based on the original query and then query, you can call the Criteria object's Createcriteria () method in the criteria object based on the query.

Example:
Session session = Sessionfactory.getcurrentsession (); User user = new user (); Transaction ts = session.begintransaction (); try {Criteria criteria1 = Session.createcriteria (Room.class); Criteria criteria2 =criterial1.createcriteria ("User"), Criteria2.add (Restrictions.eq ("name", New String ("Ijse")); User= (User) criteria.list (). get (0); Session.commit ();} catch (Hibernateexception ex) {ts.rollback (); Ex.printstacktrace ();} System.out.println (User.getname ());

  

Paging Query

A paging query is primarily about specifying two parameters: How many data to take from the first few data. You can set them by calling query or the Setfirstresult () and Setmaxresults () methods of the Criteria object.

Example:
Session session = Sessionfactory.getcurrentsession (); List userlist = null; Transaction ts = session.begintransaction (); try {     criteria = Session.createcriteria (user.class);     Criteria.setfirstresult (0);//start with the first Data     criteria.setmaxresults (10);//Fetch 10 records     userlist = (List) Criterial.list ();        Session.commit (); } catch (Hibernateexception ex) {     ts.rollback ();     Ex.printstacktrace (); }

  

Hibernate several query ways-HQL,QBC,QBE, offline query, compound query, paged query

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.