• 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
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!