Hibernate restrictions usage Summary

Source: Internet
Author: User

Method

Description

Restrictions. EQ

=

Restrictions. alleq

Use Map to impose multiple equal limits

Restrictions. gt

>

Restrictions. Ge

> =

Restrictions. lt

<

Restrictions. Le

<=

Restrictions.

Between

Restrictions. Like

Like

Restrictions. In

In

Restrictions. And

And

Restrictions. or

Or

Restrictions. sqlrestriction

Use SQL to limit queries

QBC common restricted sets:

Restrictions. EQ --> equal, equal.

Restrictions. alleq --> the parameter is a map object. Using key/value to compare multiple equal values 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 --> corresponding SQL's between clause

Restrictions. Like --> corresponding to the like clause of 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 --> string matching in the middle. 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. It 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 -----------------------------------------------------------------------------------

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.