JPA2 og CriteriaBuilder.nullif
Hej,jeg har en JPA2 entity:
-----------------------
@Entity
@Table(name = "FOO")
public class FooEntity {
@Column(name = "ID")
@Id
private Long id;
@Column(name = "SOME_TEXT")
private String someText;
// getters and setters
}
-----------------------
Så har jeg en JPA2 Criteria Query:
----------------------------------
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<FooEntity> query = cb.createQuery(FooEntity.class);
Root<FooEntity> foo = query.from(FooEntity.class);
Expression<?> someTextOrNullExpression = cb.nullif(foo.get(FooEntity_someText), "test");
query.select(foo);
TypedQuery<FooEntity> typedQuery = em.createQuery(query);
List<FooEntity> resultList = typedQuery.getResultList();
for (FooEntity fooEntity : resultList) {
System.out.println(fooEntity.getId() + "\t| " + fooEntity.getSomeText());
}
----------------------------------
Output er:
----------------------------------
1 | abc
2 | test
3 | def
----------------------------------
Er det muligt at blande min someTextOrNullExpression med mit query.select(), sådan at jeg
for følgende output uden at bruge Tuple query eller constructor expression i min criteria query?
----------------------------------
1 | abc
2 | null
3 | def
----------------------------------
Hvis ja, hvordan?
Mange tak for hjelp!
med venlig hilsen
KernelX