Hibernate Criteria restrictions.or Query loops multiple or conditions

Source: Internet
Author: User

Hibernate condition or query loop multiple or conditions

Today the company let me make a list query page, because or for hibernate unfamiliar just out of experience, code knock less, all led to all kinds of difficulties ... There is a multi-level menu bar on the left side of the page because of the need to use the or (or) in the SQL query condition, but because I am using hibernate, I have been searching online for the following code:

1  This . Createcriteria (); 2 Criteria.add (restrictions.or (Restrictions.eq ("name", "a"), Restrictions.eq ("name", "B"));

But this can only be one time to write an OR condition, my needs need me to write similar name= "a" or name= "B" or name= "C" ...
All I went on searching again, got the following code:

1  This . Createcriteria (); 2 disjunction dis = restrictions.disjunction (); 3  for (int i = 0; i < 5; i++) {4                 dis.add (Restrictions.eq ("name", "" +i)); 5 }6 criteria. Add (DIS);

Problem solved perfectly.
Restrictions.or () can be used when only one or condition is required, and restrictions.disjunction () is required when multiple or conditions are used;

This is my first blog post. Just to Shanghai to work less than half a year, hoping to make rapid progress! Come on!!!!
Alvin_, first-Sen.

Hibernate Criteria restrictions.or Query loops multiple or conditions

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.