Hibernate condition Query)

Source: Internet
Author: User

1. Create a criteria instance
Net. SF. hibernate. Criteria this interface represents a query for a specific persistence class. Session is a factory used to create a criteria instance.

Criteria crit = sess. createcriteria (Cat. Class );
Crit. setmaxresults (50 );
List cats = crit. List ();

2. narrow the range of result sets
A query condition (criterion) is an instance of the net. SF. hibernate. expression. Criterion interface. Class net. SF. hibernate. expression. expression defines some built-in criterion types.

List cats = sess. createcriteria (Cat. Class)
. Add (expression. Like (name, Fritz % ))
. Add (expression. Between (weight, minweight, maxweight ))
. List ();
Expressions can be grouped by logic.

List cats = sess. createcriteria (Cat. Class)
. Add (expression. Like (name, Fritz % ))
. Add (expression. Or (
Expression. eq (age, new INTEGER (0 )),
Expression. isnull (AGE)
))
. List ();
List cats = sess. createcriteria (Cat. Class)
. Add (expression. In (name, new string [] {Fritz, Izi, PK }))
. Add (expression. disjunction ()
. Add (expression. isnull (AGE ))
. Add (expression. eq (age, new INTEGER (0 )))
. Add (expression. eq (age, new INTEGER (1 )))
. Add (expression. eq (age, new INTEGER (2 )))
))
. List ();
There are many pre-fabricated condition types (Child classes of expression ). It is particularly useful for you to embed SQL directly.

List cats = sess. createcriteria (Cat. Class)
. Add (expression. SQL (lower ($ alias. Name) Like lower (?), Fritz %, hibernate. String ))
. List ();
{Alias} is a placeholder, which is replaced by the row alias of the queried object. (Original: The {alias} placeholder with be replaced by the row alias of the queried entity .)

3. Sort the results
You can use net. SF. hibernate. expression. Order to sort the result set.

List cats = sess. createcriteria (Cat. Class)
. Add (expression. Like (name, F %)
. Addorder (order. ASC (name ))
. Addorder (order. DESC (AGE ))
. Setmaxresults (50)
. List ();
4. Associations)
You can use createcriteria () between associations to easily specify constraints between entities with links.

List cats = sess. createcriteria (Cat. Class)
. Add (expression. Like (name, F %)
. Createcriteria (kittens)
. Add (expression. Like (name, F %)
. List ();
Note: The second createcriteria () returns a new criteria instance pointing to the elements of the kittens collection class.

The following alternative forms are useful in specific situations.

List cats =Sess. createcriteria (Cat. Class)
. Createalias (kittens, KT)
. Createalias (mate, MT)
. Add (expression. eqproperty (Kt. Name, MT. Name ))
. List ();
(Createalias () does not create a new criteria instance .)

Note that the kittens collection classes held by the cat instance in the previous two queries are not pre-filtered by criteria! If you want to return only kittens that meet the condition, you must use returnmaps ().

List cats = sess. createcriteria (Cat. Class)
. Createcriteria (kittens, KT)
. Add (expression. eq (name, F % ))
. Returnmaps ()
. List ();
Iterator iter = cats. iterator ();
While (ITER. hasnext ()){
Map map = (MAP) ITER. Next ();
Cat cat = (CAT) map. Get (criteria. root_alias );
Cat kitten = (CAT) map. Get (KT );
}
5. Dynamic Association object acquisition (Dynamic Association fetching)
You can use setfetchmode () to change the automatic acquisition policy of the associated object at runtime.

List cats = sess. createcriteria (Cat. Class)
. Add (expression. Like (name, Fritz % ))
. Setfetchmode (mate, fetchmode. Eager)
. List ();
This query will get both mate and kittens through outer join.

6. query by example (example queries)
Net. SF. hibernate. expression. Example class allows you to create query conditions from the specified instance.

Cat cat = new CAT ();
Cat. setsex ('F ');
Cat. setcolor (color. Black );
List Results = session. createcriteria (Cat. Class)
. Add (example. Create (CAT ))
. List ();
Version attribute, indicating that the character attributes and associations are ignored. By default, null attributes are excluded.

You can adjust how the example is applied. You can adjust how the example is applied.

Example example = example. Create (CAT)
. Excludezeroes () // exclude zero valued properties
. Excludeproperty (color) // exclude the property named color
. Ignorecase () // perform case insensitive string comparisons
. Enablelike (); // use like for string comparisons
List Results = session. createcriteria (Cat. Class)
. Add (example)
. List ();
You can even use examples to create criteria for the associated object.

List Results = session. createcriteria (Cat. Class)
. Add (example. Create (CAT ))
. Createcriteria (mate)
. Add (example. Create (Cat. getmate ()))
. 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.