Hello all,
I am looking for a workaround for
https://hibernate.onjira.com/browse/HHH-6686.
This is my code (kind of "find by example" query) :
Code:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Flow> criteriaQuery = criteriaBuilder
.createQuery(Flow.class);
Root<Flow> root = criteriaQuery.from(Flow.class);
criteriaQuery.select(root.alias("flow"));
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(criteriaBuilder.equal(
root.get("componentTypeReference"),
flow.getComponentTypeReference()));
predicates.add(criteriaBuilder.equal(
root.<ProductionCategory> get("productionCategory"),
flow.getProductionCategory()));
predicates.add(criteriaBuilder.equal(root.<Device> get("device"),
flow.getDevice()));
Set<Date> optimalPrintingDates = flow.getOptimalPrintingDates();
if (optimalPrintingDates != null) {
Path<Set<Date>> optimalPrintingDatesPath = root
.<Set<Date>> get("optimalPrintingDates");
if (optimalPrintingDates.isEmpty()) {
predicates.add(criteriaBuilder
.isEmpty(optimalPrintingDatesPath)); // this makes Hibernate fail
} else {
predicates.add(criteriaBuilder.equal(optimalPrintingDatesPath,
flow.getOptimalPrintingDates()));
}
}
Set<Priority> priorities = flow.getPriorities();
if (priorities != null) {
Path<Set<Priority>> prioritiesPath = root
.<Set<Priority>> get("priorities");
if (priorities.isEmpty()) {
predicates.add(criteriaBuilder.isEmpty(prioritiesPath)); // this makes Hibernate fail
} else {
predicates.add(criteriaBuilder.equal(prioritiesPath,
flow.getPriorities()));
}
}
criteriaQuery.where(predicates.toArray(new Predicate[0]));
TypedQuery<Flow> singleFlowQuery = entityManager
.createQuery(criteriaQuery);
List<Flow> foundFlows = singleFlowQuery.getResultList();
if (foundFlows == null || foundFlows.isEmpty()) {
return null;
} else if (foundFlows.size() > 1) {
throw new NonUniqueResultException("Le flux n'est pas unique : "
+ foundFlows.size());
} else {
return foundFlows.get(0);
}
How can I replace the isEmpty predicate ? I tried with join and notExists, but failed all attempts.
Thanks for any help !