hddaniel
hddaniel

Reputation: 31

CriteriaBuilder JPA 2.0 Eclipselink

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?

Upvotes: 3

Views: 2528

Answers (2)

ChrLipp
ChrLipp

Reputation: 15668

Alternatively you may want to use QueryDSL, see an introduction under http://blog.mysema.com/2010/04/querydsl-as-alternative-to-jpa-2.html.

QueryDSL is a great addon for JPA ( in your case on EclipseLink), but works also for JDO, collections, SQL/JDBC, Lucene, MongoDB....

Upvotes: 0

Gordon Yorke
Gordon Yorke

Reputation: 1996

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");

Upvotes: 3

Related Questions