The difference between HQL query and criteria query in Hibernate framework

Source: Internet
Author: User

The Hibernate framework provides HQL queries and criteria queries. Here is an example of these two queries. There is a general understanding of these two query methods. Use the Housing Information table as an example to check all housing information.

HQL Statement Query all housing information:

/** Check All houses * * (non-javadoc) * @see dao.housedao#selecthouse ()*/     PublicListSelecthouse () {//TODO auto-generated Method Stub//Get ConnectionsSession session =hibernateutil.getsession (); //Query Housing entity classString hql= "from house"; Query q=cr=session.createquery (HQL); List List=cr.list (); returnlist; }

The Criteria statement queries all housing information:

 /*   * query All houses * * (non-javadoc) * @see Dao.housedao#selecthouse ()  */ public  list Selecthouse () { // TODO auto-generated Method stub  Session session = Hibernateutil.getsession ();  //  Create criteria  criteria cr= Session.createcriteria (House.                         Class  );          List List  = Cr.list ();           return   list; }

By comparing the two, we can see:

1. TheHQL statement must write the corresponding HQL query statement, and the Criteria statement puts the entity class object directly into the class, omitting the query statement.

2,HQL query, must have a certain foundation of SQL . The Criteria query does not require any SQL Foundation.

3,HQL query than Criteria query with a broader scope, we recommend the use of HQL query. But you should also be aware of the Criteria query.

you can comment on the content of the article by clicking the button in the lower right corner, or follow the latest news on my blog by following the button on the bottom left. If the article content is helpful to you, don't forget to click the recommended button in the lower right corner   to support the 2276292708@qq. com if need to reprint, please specify the source, thank you!! 

The difference between HQL query and criteria query in Hibernate framework

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.