-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: [HHH-6686] @ElementCollection + isEmpty workaround
PostPosted: Tue Dec 27, 2011 7:13 am 
Newbie

Joined: Tue May 26, 2009 6:15 am
Posts: 4
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 !


Top
 Profile  
 
 Post subject: Re: [HHH-6686] @ElementCollection + isEmpty workaround
PostPosted: Tue Dec 27, 2011 12:42 pm 
Newbie

Joined: Tue May 26, 2009 6:15 am
Posts: 4
My apologies, there is a trivial solution :

Code:
Expression<Integer> dateCount = criteriaBuilder
      .size(optimalPrintingDatesPath);
predicates.add(criteriaBuilder.equal(dateCount, 0));


Well, an empty collection is a collection that has 0 elements.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.