Criteria Builder JPA 2.0 Eclipselink

If I want something like this with EclipseLink and JPA 2.0

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

What is the best way? The official says something 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 excluded using eclipselink because it cb.equal(pet.get(Pet_.name), "Fido")is Predicateand does not request a request with.and

Any ideas?

+3
source share
2 answers

Looking at the APIs 'and' and 'or', they are in CriteriaBuilder, so the request will look like this:

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

"name", :

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

, ( "1" ) :

cb.parameter(String.class, "value1");
+3

QueryDSL, . http://blog.mysema.com/2010/04/querydsl-as-alternative-to-jpa-2.html.

QueryDSL - JPA ( EclipseLink), JDO, , SQL/JDBC, Lucene, MongoDB....

0

Source: https://habr.com/ru/post/1762639/


All Articles