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 |
QBC commonly used limited 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 '"
Examples of all student objects aged between 20-30 years old list List = Session.createcriteria (Student.class). Add (Restrictions.between ("Age", new intege R (), New Integer (()). List (); Check student names between AAA,BBB,CCC student objects 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 ("aged")). List (); Query the student object with age equal to 20 or an empty age list = Session.createcriteria (Student.class). Add (Restrictions.or (Restrictions.eq ("Ages", NE W Integer), Restrictions.isnull ("Age")). List ();
-------------------------------------------------------------------- using QBC to implement 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 (Restri Ctions.eq ("Age", new Integer)); } criteria.addorder (Order.asc ("name"));//The Return criteria.list () in ascending order by name; }
-----------------------------------------------------------------------------------
Today, I used the restrictions (which is also possible with expression) to write Hibernate advanced queries. 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 for 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;
Hibernate restrictions Usage (reprint) Alternative expression