Let's talk about the criteria query, which is easy for us programmers who are not too familiar with SQL statements.
Don't say much nonsense, take a look at the example:
The entity classes are as follows:
public class User implements serializable{
private static final long serialversionuid = 1L;
public Long ID;
private String name;
private int age;
Omit Get/set Method
}
Mapping file We will not write a very simple entity, if you do not understand the children's shoes please refer to my other articles in the Hibernate category.
Next we'll look at how to use the criteria to query:
public static void Main (string[] args) {
Configuration cfg = new Configuration (). Configure ();
Sessionfactory sessionfactory = Cfg.buildsessionfactory ();
Session session = Sessionfactory.opensession ();
Criteria criteria = Session.createcriteria (user.class);
Criteria.add (Restrictions.eq ("name", "Shun"));
List List = Criteria.list ();
Iterator iter = List.iterator ();
while (Iter.hasnext ()) {
User user = (User) iter.next ();
System.out.println (User.getname () + ":" +user.getage ());
}
Session.close ();
}
See the code, a very simple string.
We are familiar with the following, we see the code after the session is constructed:
Criteria criteria = Session.createcriteria (user.class);
Criteria.add (Restrictions.eq ("name", "Shun"));
The two code is the focus, let's analyze it, what does it mean?
In the first sentence we get an object of the criteria implementation class by session, and then the second sentence we add a condition through the Add method, EQ represents equality. Before Hibernate3 was achieved by Expression.eq, after 3, because the criteria were discarded, we used the restrictions class to implement, it and expression the same use. Let's look at the API discovery expression inherits from restrictions.
Back to our two sentences, after we had done these things, actually hibernate helped us construct a similar
SELECT * from user where name= ' shun '
Such a statement. (The table for the user class in our mapping file is the user table, and the Name property corresponds to the Name field)
Restrictions there are a number of ways to help us construct SQL statements, and it's easy to understand the API.
Let's look at the code again, if we close the session, but we want to continue using the criteria, OK? Let's take a look.
After the code above, we iterate over, plus:
List List2 = Criteria.list ();
Iterator iter2 = List.iterator ();
while (Iter.hasnext ()) {
User user = (User) iter.next ();
System.out.println (User.getname () + ":" +user.getage ());
}
To distinguish the difference between a list and ITER, we use the other one here.
To run it, we get an exception:
Org.hibernate.SessionException:Session is closed!
The report of this exception indicates that the session has been closed, in many cases we closed the session to do saveorupdate,save and so on with the operation of the persistence will be reported similar exception.
Hibernate3 considering our needs, it implements a detachedcriteria that can exist independently of the session.
Let's take a look at the example: (Entity or above)
public static void main (string[) args) {Configuration cfg = new Configuratio
N (). Configure ();
Sessionfactory sessionfactory = Cfg.buildsessionfactory ();
Session session = Sessionfactory.opensession ();
Detachedcriteria Decriteria = Detachedcriteria.forclass (User.class);
Decriteria.add (Restrictions.eq ("name", "Shun"));
List List = Decriteria.getexecutablecriteria (session). List ();
Iterator iter = List.iterator ();
while (Iter.hasnext ()) {User user = (User) iter.next ();
System.out.println (User.getname () + ":" +user.getage ());
} session.close ();
Session Session2 = Sessionfactory.opensession ();
List list2 = Decriteria.getexecutablecriteria (session2). List ();
Iterator iter2 = List2.iterator ();
while (Iter2.hasnext ()) {User user = (User) iter2.next ();
System.out.println (User.getname () + ":" +user.getage ()); }
}
We see that we can continue to use Detachedcriteria in another connection after the session is closed. We need to associate the current Detachedcriteria with a session through Getexecutablecriteria.
Next we'll take a look at the combination of the subqueries class and Detachedcriteria:
public static void Main (string[] args) {
Configuration cfg = new Configuration (). Configure ();
Sessionfactory sessionfactory = Cfg.buildsessionfactory ();
Session session = Sessionfactory.opensession ();
Detachedcriteria Decriteria = Detachedcriteria.forclass (user.class);
Decriteria.setprojection (Projections.avg ("Age"));
Criteria criteria = Session.createcriteria (user.class);
Criteria.add (Subqueries.propertygt ("Age", Decriteria));
List List = Criteria.list ();
Iterator iter = List.iterator ();
while (Iter.hasnext ()) {
User user = (User) iter.next ();
System.out.println (User.getname () + ":" +user.getage ());
}
Session.close ();
}
It is expected that the first code should be in doubt:
Decriteria.setprojection (Projections.avg ("Age"));
This code refers to the average of the age by Decriteria. Then you get an object of age greater than the average below.
Projections contains a number of encapsulation methods to implement SQL methods, you can look at the API.
Let's take a look at a slightly more advanced usage of it.
Just look at the code:
Criteria.setfirstresult (ten);
Criteria.setmaxresults (20);
Here we set the beginning of the record is 10th, and then from the 10th start to find 20 records, according to this practice, we can achieve the basic paging functionality.
Of course, in many cases we need to sort, and the criteria are supported:
Criteria.addorder (Order.desc ("Age"));
Here, we can use the AddOrder method directly, inside through the Order.desc to get an order object, it needs an attribute parameter. In fact, when we call AddOrder, Hibernate will help us generate an order by age, such as the statement.
When we need to group, how does this work? This will need to use the Groupproperty method of the projections that we have previously involved,
Criteria.setprojection (Projections.groupproperty ("Age"));
Here we group by The age attribute, which is actually grouped by the age corresponding to the field age, and hibernate will automatically help us convert to a statement such as group by age. There are many practical methods in the
projections (note that this is only available after Hibernate 3).  &NBSP