Reprint: http://ligaosong.iteye.com/blog/1469056
1. Method description
Method |
Description |
Restrictions.eq |
= |
Restrictions.alleq |
Use map to make multiple equal limits |
Restrictions.gt |
> |
Restrictions.ge |
>= |
restrictions.lt |
The |
Restrictions.le |
<= |
Restrictions.between |
Between |
Restrictions.like |
Like |
Restrictions.in |
Inch |
Restrictions.and |
and |
Restrictions.or |
Or |
Restrictions.sqlrestriction |
Qualifying queries with SQL |
2, QBC common limit method
Restrictions.eq--Equal, equals.
Restrictions.alleq---parameters are map objects, using Key/value for multiple equals, equivalent to multiple restrictions.eq effects
RESTRICTIONS.GT-Great-than > Greater than
Restrictions.ge--great-equal >= greater than or equal
restrictions.lt--Less-than, < less than
Restrictions.le--less-equal <= less than or equal
Restrictions.between--between clause corresponding to SQL
Restrictions.like-A LIKE clause for SQL
Restrictions.in to the IN clause of the corresponding SQL
Restrictions.and---and relationships
Restrictions.or--or relationship
Restrictions.isnull---Determines whether the property is empty or null returns True
Restrictions.isnotnull--and IsNull opposite
Restrictions.sqlrestriction---SQL-qualified queries
Order.asc in ascending order based on incoming fields
Order.desc-in descending order based on incoming fields
Matchmode.exact---string exact match. Equivalent to "like ' value '"
Matchmode.anywhere--The string matches in the middle. Equivalent to "like '%value% '"
Matchmode.start--The string is in the front position. Equivalent to "like ' value% '"
Matchmode.end--The position of the string at the last face. Equivalent to "like '%value '"
Java code
example query age in-all student objects between 30 years of age list List= Session.createcriteria (Student.class). Add (Restrictions.between ("Age",NewInteger (20),NewInteger (30) . List (); Query student names between AAA,BBB,CCC string[] names= {"AAA", "BBB", "CCC"}; List List= Session.createcriteria (Student.class). Add (Restrictions.in ("Name", names)). List (); Query the student object with an empty age list List= Session.createcriteria (Student.class). Add (Restrictions.isnull ("Age") . List (), query the student object with age equal to 20 or empty age list List= Session.createcriteria (Student.class). Add (Restrictions.or (Restrictions.eq ("Age",NewInteger (20)), Restrictions.isnull ("Age") . List ();--------------------------------------------------------------------using QBC to implement dynamic queries PublicList findstudents (String name,intAge ) {Criteria= Session.createcriteria (Student.class); if(Name! =NULL) {Criteria.add (Restrictions.liek ("Name", Name,matchmode.anywhere)); } if(Age! = 0) {Criteria.add (Restrictions.eq ("Age",NewInteger (age)); } criteria.addorder (Order.asc ("Name"));//Sort by name Ascending returncriteria.list ();} -----------------------------------------------------------------------------------This class uses restrictions (of course, expression) to write Hibernate advanced queries today. It feels good. The following code is hard to read. In fact, the core is a sentence restrictions.or ( Restrictions.like (), restrictions.or (Restrictions.like,........)) The or can be infinitely added. It's better. Session Session=gethibernatetemplate (). Getsessionfactory (). Opensession (); Criteria 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 (); returnList
The usage of restrictions in hibernate criteria