The design can be flexibly based on the characteristics of the criteria to facilitate the assembly of query conditions. Here's a summary of the usage of the criteria for Hibernate: Hibernate designed 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 different and the criteria is online, so it is created by the Hibernate Session, while the Detachedcriteria is offline and created without S Ession,detachedcriteria provides 2 static methods Forclass (Class) or Forentityname (Name) for Detachedcriteria instance creation. The Spring Framework provides the gethibernatetemplate (). Findbycriteria (Detachedcriteria) method to easily return query results based on Detachedcriteria. Criteria and Detachedcriteria can be used to set query conditions using Criterion and Projection. You can set the Fetchmode (the mode of federated query fetching) and set the sorting method. For Criteria, you can also set Flushmodel (the way to flush sessions) and Lockmode (Database lock mode). 1:
Detachedcriteria Beautycriteria =detachedcriteria.forclass (beauty.class). Createcriteria ("Customers");
Beautycriteria.add (Restrictions.eq ("name", "Gates")):
2:
Detachedcriteria Beautycriteria =detachedcriteria.forclass (beauty.class). CreateAlias ("Customers", "C");
Beautycriteria.add (Restrictions.eq ("C.name", "Gates")):
Then there are new requirements, too old beauty do not, or to find a client gates, the conditions are as follows:
Detachedcriteria Beautycriteria =detachedcriteria.forclass (Beauty.class, "B").;
Detachedcriteria Customercriteria =beautycriteria.createalias ("Customers", C ");
Beautycriteria.add (Restrictions.le ("B.age", New Long (20)):
Customercriteria.add (Restrictions.eq ("C.name", "Gates")):
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use of Detachedcriteria