views:

153

answers:

1

If I want something like that with EclipseLink and JPA 2.0

SELECT ... FROM ... WHERE name1=value1 AND name2=value2 OR name3=value3

Which is the best way?? In the oficial say somthing like:

cq.where(cb.equal(pet.get(Pet_.name), "Fido")
    .and(cb.equal(pet.get(Pet_.color), "brown");

http://download.oracle.com/javaee/6/tutorial/doc/gjivm.html#gjiwu

but is imposible with eclipselink because cb.equal(pet.get(Pet_.name), "Fido") is a Predicate and not anidate query with .and

Any ideas?

+2  A: 

Looking at the API 'and' and 'or' operators are on the CriteriaBuilder so the query would look like: cq.where(cb.and(cb.equal(pet.get(Pet_.name), "Fido"), cb.equal(pet.get(Pet_.color), "brown")));

Using the "name" example where clause the call would be:

cq.where(cb.or(cb.and(cb.equal(BeanName_.name1, "value1"), cb.equal(BeanName_name2, "value2")), cb.equal(BeanName_.name3, "value3")));

if you wanted to use parameters simply replace the hard coded values (ie. "value1") with parameters : cb.parameter(String.class, "value1");

Gordon Yorke