Biggy_java2
Biggy_java2

Reputation: 1951

Hibernate criterion

Criteria criteria = getSession().createCriteria(clazz); 
Criterion rest1= Restrictions.and(Restrictions.eq("A", "X"), 
       Restrictions.in("B", Arrays.asList("X","Y")));
Criterion rest2= Restrictions.and(Restrictions.eq("A", "Y"), 
       Restrictions.eq("B", "Z"));
criteria.add(Restrictions.or(rest1, rest2));

How to create criterion if there are odd number of AND parameters eg. 5?

Upvotes: 1

Views: 1906

Answers (2)

antlersoft
antlersoft

Reputation: 14751

Just take the result of a previous call to Restrictions.and() as one of the parameters to Restrictions.and()

For example:

Criterion cr = Restrictions.and(Restrictions.and(Restrictions.eq("A", "X"), 
       Restrictions.in("B", Arrays.asList("X","Y"))), Restrictions.eq("C", "Z));

Upvotes: 2

Alex
Alex

Reputation: 11579

You can use DetachedCriteria and it will look like:
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(rest1);
disjunction.add(rest2);
disjunction.add(rest3);
...
dc.add(disjunction);

For AND you can use Conjunction instead of Disjunction.

Upvotes: 4

Related Questions