Recently, Spring and Hibernate were used in the project for development. Thanks to Criteria, the query method design allows you to flexibly assemble query conditions according to the features of Criteria. Now we will summarize the usage of Hibernate Criteria: Hibernate designed CriteriaSpecification as the parent interface of Criteria. The following provides Criteria and DetachedCriteria.
The major difference between Criteria and DetachedCriteria is that Criteria is created in different forms. Criteria is online, so it is created by Hibernate Session. DetachedCriteria is offline and does not need to be created, detachedCriteria provides two static methods forClass (Class) or forEntityName (Name) to create a DetachedCriteria instance.
The Spring framework provides the getHibernateTemplate (). findByCriteria (detachedCriteria) method to conveniently return the query results based on DetachedCriteria.
Criteria and DetachedCriteria both use Criterion and Projection to set query conditions. You can set FetchMode (the mode of joint query capture) and set the sorting mode. For Criteria, you can also set FlushModel and LockMode ). The following describes Criterion and Projection in detail.
Criterion is the query condition for Criteria. Criteria provides the add (Criterion criterion) method to add query conditions. The Criterion interface mainly includes Example, Junction, and SimpleExpression. The actual use of Junction is its two sub-classes conjunction AND disjunction, which use the and or operators to join the query condition set. Criterion instances can be created through the Restrictions tool class. Restrictions provides a large number of static methods, such as eq (equal to) and ge (greater than or equal) and between to create the Criterion query condition (SimpleExpression instance ). In addition, Restrictions also provides methods to create conjunction and disjunction instances. You can add query conditions to the add (Criteria) method of the instance to form a set of query conditions. As for the creation of Example, Example itself provides a static method of create (Object entity), which is created based on an Object (usually mapped to a database Object in actual use. Then you can set some filter conditions:
- Example exampleUser = Example. create (u). ignoreCase ()// Case insensitive
- . EnableLike (MatchMode. ANYWHERE );// For the String type attribute, the value matches wherever it is. Equivalent to % value %
Project mainly enables Criteria to query reports and implement grouping. Project has three major implementations: SimpleProjection, ProjectionList, and Property. SimpleProjection and ProjectionList are instantiated by built-in Projections. For example, the provided avg, count, max, min, and sum allow developers to easily perform statistical queries on a field. Property is the setting of query conditions for a field, such as through Porperty. forName ("color "). in (new String [] {"black", "red", "write"}); you can create a Project instance. Use the add (Project) method of criteria to join the query conditions. When using Criteria for query, it is important to note that Hibernate provides the classes and methods to meet the creation and assembly of the Development query conditions. The following describes several usage methods: 1. create a Criteria instance org. hibernate. the Criteria interface indicates a query of a specific persistent class. Session is the factory of the Criteria instance.
- Criteria crit = sess. createCriteria (Cat.Class);
- Crit. setMaxResults (50);
- List cats = crit. list ();
2. A separate query condition for restricted result set content is an instance of the org. hibernate. criterion. Criterion interface. The org. hibernate. criterion. Restrictions class defines factory methods for obtaining some built-in Criterion types.
- List cats = sess. createCriteria (Cat.Class). Add (Restrictions. like ("Name","Fritz %"))
- . Add (Restrictions. ("Weight", MinWeight, maxWeight). list ();
Constraints can be grouped by logic.
- List cats = sess. createCriteria (Cat.Class). Add (Restrictions. like ("Name","Fritz %"))
- . Add (Restrictions. or (Restrictions. eq ("Age",NewInteger (0), Restrictions. isNull ("Age"). List ();
- List cats = sess. createCriteria (Cat.Class)
- . Add (Restrictions. in ("Name",NewString [] {"Fritz","Izi","Pk"}))
- . Add (Restrictions. disjunction (). add (Restrictions. isNull ("Age"))
- . Add (Restrictions. eq ("Age",NewInteger (0)))
- . Add (Restrictions. eq ("Age",NewInteger (1)))
- . Add (Restrictions. eq ("Age",NewInteger (2)))))
- . List ();