Use of hibernate criteria

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


Criteria query = hibernateutil. getsession (). createcriteria (entity. Class );
If (ID> 0)
{
Query. Add (restrictions. eq (ID,
Entity. GETID ()));
}
If (title! = NULL)
{
Query. Add (restrictions. Like (title,
Entity. gettitle (), matchmode. Anywhere ));
}
If (entity. getstatus ()>-1)
Query. Add (restrictions. In (status, arrayutils. toobject (New int [] {0, 1 })));
}
If (entity. getendtime ()> 0 &&

Entity. getstarttime ()> 0)
{
Query. Add (restrictions. Or (
Restrictions. Between (start_time,
Entity. getstarttime (),
Entity. getendtime ()),
Restrictions. Between (end_time,
Entity. getstarttime (),
Entity. getendtime ())));

// Query. Add (restrictions. Ge (start_time,
Entity. getstarttime ()));
// Query. Add (restrictions. Le (end_time,
Entity. getendtime ()));
}

Query. addorder (order. ASC (start_time ));
Query. Add (restrictions. sqlrestriction ("{alias}. ID in (select ID from user where manager_id =" + managerid + ")"));

Return query. List ();

Note:

Hibernate query, which learns to build a criteria query, In the criteria Query

Use the sqlrestriction () method to provide SQL syntax for restricted queries, such:

Criteria
Criteria = session. createcriteria (user. Class); criteria. Add (restrictions. sqlrestriction ("{alias}. Name
Like (?) "," Cater % ", hibernate. String); List users = criteria. 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.