Use of detachedcriteria in hibernate)

Source: Internet
Author: User
Criteria locks SQL statements so that developers can use objects to perform operations on the data. For example, the following queries all the data in the User table: Criteria = session. createcriteria (user. Class );
// Query all buckets of the specified user
List users = criteria. List ();
Iterator = users. iterator ();
System. Out. println ("ID \ t Name/age ");
While (iterator. hasnext ()){
User user = (User) iterator. Next ();
System. Out. println (user. GETID () +
"\ T" + User. getname () +
"/" + User. getage ());
}

In hibernate, use the following SQL statement to check the metadata: select this _. ID as id0 _, this _. name as name0_0 _, this _. age as age0_0 _ from user this _

Criteria is actually just a container. To set the check condition, add the restrictions condition using the add () method, for example, you can check the information with a snapshot age greater than 20 and less than 40:Criteria = session. createcriteria (user. Class );
Criteria. Add (restrictions. gt ("Age", new INTEGER (20 )));
Criteria. Add (restrictions. LT ("Age", new INTEGER (40 )));
List users = criteria. List ();

You can also use a condition combination to query conditions. For example, conditions such as combination age for (EQ) 20 or (OR) age for null (isnull) are as follows:Criteria = session. createcriteria (user. Class );
Criteria. Add (restrictions. Or (
Restrictions. eq ("Age", new INTEGER (20 )),
Restrictions. isnull ("Age ")
));
List users = criteria. List ();

You can also use the sqlrestriction () method to provide the SQL Limit Method for limit query. For example, you can query the information starting with a cater:Criteria = session. createcriteria (user. Class );
Criteria. Add (restrictions. sqlrestriction ("{alias}. name like (?) "," Cater % ", hibernate. String ));
List users = criteria. List ();

In this example, alias will be replaced with a user-dependent name, while? It will be replaced by cater %, that is, the value provided by the second vertex. When creating an SQL statement, you do not have to specify where. If there are multiple query conditions, for example, the query condition of the between clause can be as follows: Criteria = session. createcriteria (user. Class );
Integer [] ages = {New INTEGER (20), new INTEGER (40 )};
Type [] types = {hibernate. integer, hibernate. Integer };
Criteria. Add (restrictions. sqlrestriction ("{alias}. Age (?) And (?) ", Ages, types ));
List users = criteria. List ();

The following table lists the methods for checking the limits of restrictions:

Method Description
Restrictions. EQ Wait
Restrictions. alleq Use Map and key/value to perform multiple equal comparison
Restrictions. gt Greater than>
Restrictions. Ge Greater than> =
Restrictions. lt Smaller than <
Restrictions. Le Less than equal to <=
Restrictions. Between clause corresponding to SQL
Restrictions. Like Like clause corresponding to SQL
Restrictions. In In clause corresponding to SQL
Restrictions. And And relationship
Restrictions. or Or relationship
Restrictions. sqlrestriction SQL limit Query

 

 

====================

 

Hibernate criteria association query

As mentioned above, criteria looks much more eye-catching than hql and continues.

If every beauty has its own customer resources (don't worry about it !), What should I do if I need to query the beauty of the customer's gates?

There are two methods to use criteria:

1:
Detachedcriteria beautycriteria = detachedcriteria. forclass (beauty. Class). createcriteria ("customers ");
Beautycriteria. Add (restrictions. eq ("name", "Gates ")):

2:
Detachedcriteria beautycriteria = detachedcriteria. forclass (beauty. Class). createalias ("MERs", "C ");
Beautycriteria. Add (restrictions. eq ("C. Name", "Gates ")):

Next, we had a new requirement. If you are too old to be a pretty girl, you still need to find the customer who owns gates. The conditions are as follows:
Detachedcriteria beautycriteria = detachedcriteria. forclass (beauty. class, "B ").;
Detachedcriteria customercriteria = beautycriteria. createalias ("MERs", C ");
Beautycriteria. Add (restrictions. Le ("B. Age", new long (20 ))):
Customercriteria. Add (restrictions. eq ("C. Name", "Gates ")):

For more information about criteriaSource codeAnd testing is the best document.

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.