Referring to hibernate queries, we tend to think of HQL, who makes our SQL statements object-oriented. In fact, a closer look at, is almost the SQL statement in the table and the field with the corresponding entities and attributes to replace. In fact, there is another way to query hibernate, we do not see the shadow of the task SQL, that is, criteria Query.
Criteria is a more object-oriented query method than HQL. --Baidu Encyclopedia
First, Brief introduction
Criteria query encapsulates the data query criteria into an object through an object-oriented design. In short, Criteria query can be seen as an object representation of traditional SQL, such as:
Criteria Criteria=session.createcriteria (Tuser.class), Criteria.add (Expression.eq ("name", "Zhanglianhai"); Criteria.add (Expression.eq ("Sex", New Integer (1));
The criteria instance here is essentially an encapsulation of SQL "SELECT * from T_user where name= ' Erica ' and sex=1". Hibernate generates the appropriate SQL statement at run time based on the query criteria specified in the criteria. Of course, to use the criteria, you need to know his letter symbols, such as: eq means "=", qt means ">", le means "<=" ...
Second, the commonly used related classes
1. Example Class
The example class implements the criteria interface and, similarly, it can be used as a criteria query. The function of the example is to find other objects that match the attributes according to the existing objects.
Criteria criteria=session. Createcriteria (Tuser.class); TUser exampleuser=new TUser () exapleuser.getname ("Erica"); Criteria.add (Example.create (Exampleuser)); List<tuser> list=criteria.list (); for (TUser user:list) { System.out.println (User.getname ());}
This means that you create a new Tuser object Exampleuser, and as a template, query all the records with the same name attribute.
2. Detachedcriteria
In Hibernate2, the criteria life cycle is in the life cycle of its host session, that is, a criteria instance created by a session that, once destroyed, is invalidated. The introduction of Detachedcriteria,detachedcriteria in Hibernate3 can be isolated from the session instance, so that we can pull out some common criteria query conditions, Bind to the current session instance for better code reuse each time it is used.
Detachedcriteria Decriteria=detachedcriteria.forclass (Tuser.class);d Ecriteria.add (Expression.eq ("name", "Erica") );d Ecriteria.add (Expression.eq ("Sex", New Integer (1)); Criteria Criteria=decriteria.getexecutablecriteria (session); List<tuser> list=criteria.list (); for (TUser user:list) { System.out.println (User.getname ());}
The lifetime of the Detachedcriteria is independent of the session instance, and we can create the Detachedcriteria instance independently and bind to the session when it is needed to get a run-time criteria instance. In this way, we can separate the query logic from the criteria instance for maximizing the reuse effect of the code.
Detachedcriteria can also be used for subquery representations:
Detachedcriteria Aveage=detachedcriteria.forclass (Tuser.class); Avgage.setprojection (Projections.avg ("Age")); Criteria Criteria=session.createcriteria (Tuser.class); Criteria.add (Subqueries.propertygt ("Age", AvgAge));
By subqueries we can incorporate Detachedcriteria into the query expression, which is reflected in SQL as a typical subquery statement. The SQL statement generated by the example above is roughly the following: select ... from T_user where > (select AVG (age) from T_user)
Third, advanced features
1. Limit the range of records returned
The Criteria.setfirstresult/setmaxresults method allows you to limit the range of records returned by a single query:
Criteria Criteria=session.createcriteria (Tuser.class);//Limited query returns results from 100 results, starting from 20 Records Criteria.setfirstresult (100) ; Criteria.setmaxresults (20);
2. Record sorting
Criteria Criteria=session.createcriteria (Tuser.class); Criteria.add (Expression.eq ("GroupId", New Integer (2)); Criteria.addorder (ORDER.ASC ("name")); Criteria.addorder (Order.desc ("GroupId"))
3. Grouping and statistics
In Hibernate3, we can also use criteria to complete grouping and statistics. Grouping, statistical expressions are encapsulated by the newly introduced projections class Hibernate3.
Criteria Criteria=session.createcriteria (Tuser.class); Criteria.setprojection (Projections.groupproperty ("Age")); List<tuser> list=criteria.list (); for (TUser user:list) { System.out.println (user);}
The previous example groups the current Tuser records by age. With the Projections.groupproperty method, we specified the target attribute "age" for grouping. Generated SQL statement: Select This.age as y0_ from T_user This_group by This_.age.
In addition, for the statistics and grouping functions of the multi-conditional combination, we can use Projectionlist to complete, in the following example, we have counted the number of users in each age level:
Projectionlist projectionlist=projections.projectionlist ();p Rojectionlist.add (Projections.groupproperty ("Age")) ;p RojectionList.add9Projections.rowCount ()); Criteria Criteria=session.createcriteria (Tuser.class); criteria.setprojection (projectionlist);
Iv. Brief commentary
Kind of query way to compare object-oriented, I also prefer to use this query method, because it does not have to write any SQL or HQL statements. The criteria provided by the criteria are already able to meet the basic query requirements, but the complex query is still not how to make sense.
Criteria--hibernate Object-oriented query