Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate - empty collections don't work with Criterias

 
christopher kai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to select Users that belong to Public groups or don't belong to any groups at all. user<->group is a many to many assiociation. (user have a collection of groups associated or empty if doesn't belong to any). I have to use detachedCriterias, so I do as follows:

DetachedCriteria crit = DetachedCriteria.forClass(User.class, "usr");
crit.createAlias("groups", "usrgroups");

Disjunction dis1 = Restrictions.disjunction();
dis1.add(Property.forName("usr.groups").isEmpty());
dis1.add(Property.forName("usrgroups.isPublic").eq(new Boolean(true)));

crit.add(dis1);

...
but this doesn't work, since creating alias on collection 'groups' eliminates users which have no groups (calling isEmpty() gives zero results). How to overcome that?


regards,
Chris
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since you are using detached criterias i think it is H3 you are using.

this is a common problem (see bug tracking for hibernate) but "not a bug". I don't know if the patch has been included in the latests releases (which version are you using ?) but you can take a look at: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1137

if the patch is not applied to H3 and you do not want to / can not apply the patch then you have to use HQL.

pascal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic