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