QBC (query by criteria) mainly consists of criteria, criterion, Oder, restrictions class
Session = This. getsession ();
Criteria CRI = session. createcriteria (jditemserialnumber. Class );
Criterion cron = restrictions. Like ("customer", name );
CRI. Add (cron );
List = CRI. List ();
====================================
Comparison Operators
Hql operator QBC operator meaning
= Restrictions. eq () equals
<> Restrictions. Not (exprission. eq () is not equal
> Restrictions. gt () is greater
> = Restrictions. Ge () is greater than or equal
<Restrictions. LT () is smaller
<= Restrictions. Le () is less than or equal
Is null restrictions. isnull () is null
Is not null restrictions. isnotnull () non-null
Like restrictions. Like () string mode matching
And restrictions. And () logic and
And restrictions. conjunction () logic and
Or restrictions. Or () logic or
Or restrictions. disjunction () logic or
Not restrictions. Not () logical non-
In (list) restrictions. In () is equal to a value in the list.
ONT in (list) restrictions. Not (restrictions. In () is not equal to any value in the list
Any value in between x and y restrictions. Between () closed range XY
Not between x and y restrictions. Not (Restrictions .. between () is smaller than the value x or greater than the value Y
Common Operations:
16.1. Create a criteria instance
The Org. hibernate. 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 ();
16.2. Restrict result set content
A separate query condition 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. Between ("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", new INTEGER (0 )), Restrictions. isnull ("Age ") )
) . List ();
List cats = sess. createcriteria (Cat. Class) . Add (restrictions. In ("name", new string [] {"fritz", "Izi", "Pk "})) . Add (restrictions. disjunction () . Add (restrictions. isnull ("Age ")
) . Add (restrictions. eq ("Age", new INTEGER (0 ))) . Add (restrictions. eq ("Age", new INTEGER (1 ))) . Add (restrictions. eq ("Age", new INTEGER (2 ))) )) . List ();
Hibernate provides a considerable number of built-in criterion types (Restrictions subclass), but it is particularly useful to allow you to use SQL directly.
List cats = sess. createcriteria (Cat. Class) . Add (restrictions. SQL ("lower ({alias}. Name) Like lower (?) "," Fritz % ", hibernate. String )) . List ();
The {alias} placeholder should be replaced with the column alias of the queried object.
A property instance is another way to obtain a condition. You can create a property by calling property. forname.
Property age = property. forname ("Age"); List cats = sess. createcriteria (Cat. Class) . Add (restrictions. disjunction () . Add (age. isnull ()) . Add (age. eq (New INTEGER (0 ))) . Add (
Age. eq (New INTEGER (1 ))) . Add (age. eq (New INTEGER (2 ))) )) . Add (property. forname ("name"). In (New String [] {"fritz", "Izi", "Pk "})) . List ();
16.3. sorting result sets
You can use org. hibernate. criterion. Order to sort the query results.
List cats = sess. createcriteria (Cat. Class) . Add (restrictions. Like ("name", "F % ") . Addorder (order. ASC ("name ")) . Addorder (order. DESC ("Age ")) . Setmaxresults (50) . List ();
List cats = sess. createcriteria (Cat. Class) . Add (property. forname ("name"). Like ("F % ")) . Addorder (property. forname ("name"). ASC ()) . Addorder (property. forname ("Age"). DESC ()) . Setmaxresults (50) . List ();