-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to compare two sets
PostPosted: Fri Oct 27, 2006 3:30 pm 
Newbie

Joined: Fri Oct 27, 2006 3:17 pm
Posts: 2
Location: Prague, Czech Republic
Hi.

I use hibernate 3.2 and I try find out how is possible to compare two sets in HQL. I have two classes:

class A {
Set<User> users;
...
}

class B {
Set<User> users;
...
}

with appropriate mapping in .hbm. My goal is to select all objects of class 'A'
which all of users are contained in collection of users of class 'B'.

Something like this (pseudo query):
select a from A as a, B as b where b.id = 'xy' and b.users.containsAll(a.users)

How it can be done with hibernate?

Thank for any help
Filip


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 3:58 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
well, you can either do it in memory, or do it on the database side. for the database side just use regular hql or sql. That whole b.users.containsAll(a.users) thing is most likely not be supported.

Code:
select users where <attributes that match A> and <attributes that match B>


to do it in memory, just load up both the sets and run a little loop.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 4:24 pm 
Newbie

Joined: Fri Oct 27, 2006 3:17 pm
Posts: 2
Location: Prague, Czech Republic
kochcp wrote:
well, you can either do it in memory, or do it on the database side. for the database side just use regular hql or sql. That whole b.users.containsAll(a.users) thing is most likely not be supported.

Code:
select users where <attributes that match A> and <attributes that match B>


to do it in memory, just load up both the sets and run a little loop.


Thank, I can do it, but it is not good solution for me because the database will
contain thousands of instances of both classes. Can you explain how to do it on the database side, please ?

My goal is not to select users but instances of class 'A' .


Last edited by filip26 on Tue Oct 31, 2006 8:57 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 29, 2006 5:09 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
b.users.containsAll(a.users) means that a.users is a SUBSET of b.users - not that the sets are equal. Is this your requirement?

If so, try this (xlate to formal syntax):

select A.id from A where
exists (
(select from b_users where bid='xy') minus (select from a_users where aid = a.id)
)

note: I assumed that each user has a parent_id (aid or bid)


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