ArticleDirectory
Learning about criteria of Hibernate
Address: http://xuganggogo.javaeye.com/blog/440078
- Method description
Method |
Description |
restrictions. EQ |
= |
restrictions. alleq |
Use Map to impose multiple equal limits |
restrictions. gt |
> |
restrictions. ge |
= |
restrictions. Lt |
|
restrictions. Le |
<= |
restrictions. Between |
between |
restrictions. Like |
like |
restrictions. in |
in |
restrictions. And |
and |
restrictions. Or |
or |
Restrictions. sqlrestriction |
Use SQL to limit queries |
2. QBC common limits
Restrictions. EQ--> Equal, equal.
Restrictions. alleq--> The parameter is a map object, and multiple equal comparisons are performed using key/value, which is equivalent to multiple restrictions. EQ.
Restrictions. gt--> Great-than> greater
Restrictions. Ge--> Great-equal >=greater than or equal
Restrictions. lt--> Less-than, <less
Restrictions. Le--> Less-equal <= less than or equal
Restrictions.--> Between clause corresponding to SQL
Restrictions. Like--> Like clause corresponding to SQL
Restrictions. In--> Corresponding SQL in Clause
Restrictions. And--> And relationship
Restrictions. or--> Or relationship
Restrictions. isnull--> Determines whether the attribute is null. If it is null, true is returned.
Restrictions. isnotnull--> Opposite to isnull
Restrictions. sqlrestriction--> SQL-qualified Query
Order. ASC--> Sort by input fields in ascending order
Order. DESC--> Sort by input fields in descending order
Matchmode. Exact--> Exact string matching. equivalent to "like 'value '"
Matchmode. Anywhere--> The string matches in the middle. It is equivalent to "like '% value % '"
Matchmode. Start--> The position of the string at the beginning. It is equivalent to "like 'value % '"
Matchmode. End--> The position of the string at the end is equivalent to "like '% value '"
Example
Query all students aged between 20 and 30
List list = session. createcriteria (student. Class)
. Add (restrictions. Between ("Age", new INTEGER (20), new INTEGER (30). List ();
Query the student objects whose names are AAA, BBB, and CCC.
String [] names = {"AAA", "BBB", "CCC "};
List list = session. createcriteria (student. Class)
. Add (restrictions. In ("name", names). List ();
Query student objects with an empty age
List list = session. createcriteria (student. Class)
. Add (restrictions. isnull ("Age"). List ();
Query the students whose age is 20 or whose age is null
List list = session. createcriteria (student. Class)
. Add (restrictions. Or (restrictions. eq ("Age", new INTEGER (20 )),
Restrictions. isnull ("Age"). List ();
--------------------------------------------------------------------
Use QBC for dynamic query
Public list findstudents (string name, int age ){
Criteria = session. createcriteria (student. Class );
If (name! = NULL ){
Criteria. Add (restrictions. liek ("name", name, matchmode. Anywhere ));
}
If (age! = 0 ){
Criteria. Add (restrictions. eq ("Age", new INTEGER (AGE )));
}
Criteria. addorder (order. ASC ("name"); // sort by name in ascending order
Return criteria. List ();
}
Bytes -----------------------------------------------------------------------------------
Today I used restrictions when writing hibernate advanced queries (of course expression can also be used). This class feels good.
The followingCodeWriting is not easy to read. In fact, the core is a sentence.
Restrictions. Or (restrictions. Like (), restrictions. Or (restrictions. Like ,........))
The or in it can be infinitely added. It is better to use it.
Session session = gethibernatetemplate (). getsessionfactory ()
. Opensession ();
Criteria = session. createcriteria (film. Class );
List <film> List = criteria. Add (
Restrictions. Or (restrictions. Like ("Description", key, matchmode. Anywhere ),
Restrictions. Or (restrictions. Like ("name", key, matchmode. Anywhere ),
Restrictions. Or (restrictions. Like ("direct", key, matchmode. Anywhere ),
Restrictions. Or (restrictions. Like ("mainplay", key, matchmode. Anywhere ),
Restrictions. Like ("filearea", key, matchmode. Anywhere). List ();
Session. Close ();
Return list;