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