-->
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.  [ 5 posts ] 
Author Message
 Post subject: Navigating within a set with a Restriction
PostPosted: Wed Dec 16, 2009 7:11 am 
Beginner
Beginner

Joined: Tue Aug 26, 2008 4:24 pm
Posts: 29
I have an hbm file, for example Person.hbm. I have a set in this hbm file called family with will contain Person objects that will be taken from another table. So I want with a Restriction or something similiar to get from the table Person those who doesnt have family, I mean ,those who the set family is empty. How do that?
Thanks


Top
 Profile  
 
 Post subject: Re: Navigating within a set with a Restriction
PostPosted: Wed Dec 16, 2009 12:39 pm 
Newbie

Joined: Wed Dec 16, 2009 12:24 pm
Posts: 6
Location: Munich
With criteria it should works like this.

Code:
Criteria criteria = session.createCriteria(Person.class);
criteria.add(Restrictions.isEmpty("family");
criteria.list();


For deeper navigation via collections i would recommend http://blog.bambo.it/2009/12/generic-subcriterias-for-hibernate.html


Top
 Profile  
 
 Post subject: Re: Navigating within a set with a Restriction
PostPosted: Thu Dec 17, 2009 4:32 am 
Beginner
Beginner

Joined: Tue Aug 26, 2008 4:24 pm
Posts: 29
Ok, thanks. And could I insert a condition for the set?, I mean, for example: I want to know if the set is empty of persons who have name 'Peter.' Thanks


Top
 Profile  
 
 Post subject: Re: Navigating within a set with a Restriction
PostPosted: Thu Dec 17, 2009 6:24 am 
Beginner
Beginner

Joined: Tue Aug 26, 2008 4:24 pm
Posts: 29
I have seen I can use the where clause in the Set so I added in this clause the next: where= "person_name='Peter'" but the sql doesnt generate this condition in the where clause. I have read that this is a bug but I am not sure I am doing all correctly. Thanks


Top
 Profile  
 
 Post subject: Re: Navigating within a set with a Restriction
PostPosted: Thu Dec 24, 2009 7:06 am 
Newbie

Joined: Wed Dec 16, 2009 12:24 pm
Posts: 6
Location: Munich
With Criteria it's pretty easy. You only have to add further Restrictions.
Code:
Criteria criteria = session.createCriteria(User.class);
//criteria.add(Restrictions.and(Restrictions.isEmpty("addresses"), Restrictions.eq("name", "Bambo") ));
criteria.add(Restrictions.isEmpty("addresses"));
criteria.add(Restrictions.eq("name", "Bambo"));
criteria.list();


It's the same result if you use Restrictions.and() or adding more Restrications with criteria.add().


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.