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 !!