What is the difference between HQL query and Criteria query in The Hibernate framework? hibernatehql

Source: Internet
Author: User

What is the difference between HQL query and Criteria query in The Hibernate framework? hibernatehql

The Hibernate framework provides HQL query and Criteria query. The following is an example of the two types of queries. Or have a rough understanding of these two query methods. The housing information table is used as an example to query information about all houses.

 

The HQL statement queries information about all houses:

/** Query all houses ** (non-Javadoc) * @ see Dao. houseDao # selecthouse () */public List <House> selecthouse () {// TODO Auto-generated method stub // get connection Session session = HibernateUtil. getSession (); // Query the House object class String hql = "from House"; Query q = cr = session. createQuery (hql); List list = cr. list (); return list ;}

The Criteria statement queries information about all houses:

/** Query all houses ** (non-Javadoc) * @ see Dao. houseDao # selecthouse () */public List <House> selecthouse () {// TODO Auto-generated method stub Session session = HibernateUtil. getSession (); // create the Criteria object Criteria cr = session. createCriteria (House. class); List list = cr. list (); return list ;}

By comparing the two, we can see that:

1. The HQL statement must write the corresponding HQL query statement, while the Criteria statement directly puts the object class object into the class, omitting the query statement.

2. HQL queries must have a certain SQL Foundation. Criteria queries do not require any SQL basics.

3. HQL queries have a wider range than Criteria queries. We recommend that you use HQL queries. But you also need to understand the Criteria query.

You can click the button in the lower-right corner to comment on the content of the article, or click the follow button in the lower-left corner to follow the latest developments in my blog. If the content of the article is helpful to you, do not forget to click the recommendation button in the lower right corner to support it. If you have any questions about the content of the article, you can contact me by commenting or sending an email: if you need to reprint 2276292708@qq.com, please indicate the source, thank you !!

 

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.