Recently, the use of Spring and Hibernate in the project development, feeling that the criteria are more useful, in the query method design can be flexible according to the characteristics of the criteria to facilitate the assembly of query conditions. Now summarize the use of the criteria for hibernate:
Hibernate designed the Criteriaspecification as the parent interface for criteria, and the criteria and Detachedcriteria are provided below.
The main difference between criteria and Detachedcriteria is that the form is created differently, the criteria is online, so it is created by the Hibernate session, and Detachedcriteria is offline, The creation of a Detachedcriteria instance is done without Session,detachedcriteria with 2 static methods Forclass (Class) or Forentityname (Name). The Spring Framework provides a gethibernatetemplate (). Findbycriteria (Detachedcriteria) method to easily return query results based on Detachedcriteria.
Both criteria and Detachedcriteria can use criterion and projection to set query criteria. You can set the Fetchmode (the mode of federated query fetching) to set the Sort method. For Criteria You can also set Flushmodel (the way the session is washed) and Lockmode (Database lock mode).
Criterion and projection are described in detail below.
Criterion is the criteria for the criteria query. The criteria provides an add (criterion criterion) method to add a query condition.
The main implementations of the criterion interface include: Example, Junction, and SimpleExpression. The actual use of Junction is its two subclasses conjunction and disjunction, respectively, using the and and or operators to join the query criteria collection.
Criterion instances can be created by the restrictions tool class, restrictions provides a number of static methods, such as EQ (equals), GE (greater than equal), between, etc. to create criterion query conditions (simple Expression instance). In addition, restrictions also provides a way to create conjunction and disjunction instances, by adding query criteria to the Add (Criteria) method of the instance to form a collection of query criteria.
As for the creation of Example, the Example itself provides a static method create (object entity), which is created from an object that is typically mapped to a database in its actual use. You can then set some filter conditions:
Example exampleuser =example.create (u)
. IgnoreCase ()//Ignore case
. Enablelike (Matchmode.anywhere);
Attributes of type String, regardless of where the value is matched there. The equivalent of%value% Project is to allow Criteria to make report queries and to implement groupings. Project has simpleprojection, projectionlist, and property three implementations. Where Simpleprojection and projectionlist are instantiated by built-in projections, such as AVG, COUNT, max, Min, sum can make it easy for developers to make statistical inquiries about a field.
The property is a setting for querying a field, such as through Porperty.forname ("color"). In (New string[]{"Black", "Red", "write"}); You can create a Project instance. Add the criteria to the query criteria by adding the Project method.
Using Criteria for querying, it is important to be clear that Hibernate provides those classes and methods to meet the creation and assembly of query conditions in development, and here are several uses:
1. Create a criteria instance
The Org.hibernate.Criteria interface represents a query for a particular persisted class. The session is the factory for the criteria instance.
Criteria crit = sess.createCriteria(Cat.class);
crit.setMaxResults(50);
List cats = crit.list();