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.  [ 7 posts ] 
Author Message
 Post subject: Hql distinct?
PostPosted: Tue Mar 11, 2008 11:57 am 
Beginner
Beginner

Joined: Sat Jan 15, 2005 4:50 am
Posts: 23
Hi!

I have Parent object, which has a collection of Child objects. Child has an attribute attr1.

I need those Parent objects, which has a Child that has an attr specified by a parameter.

My try:

String query = "from Parent r where r in (select a.parent from Child a where a.attr1=:attr1)";

The problem is that this query can return the same Parent object several times. I need a solution where there is a distinct on Parent, so if there are duplicates, i only get one of those.

I tried "select distinct r from parent r....." but it not works.

Secound, i need the query to be as fast as possible, so if there is a solution that is faster than the above, please give me the details.

Thanks.


Top
 Profile  
 
 Post subject: Re: Hql distinct?
PostPosted: Tue Mar 11, 2008 12:34 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Gabor Beres wrote:
Hi!

I have Parent object, which has a collection of Child objects. Child has an attribute attr1.

I need those Parent objects, which has a Child that has an attr specified by a parameter.

My try:

String query = "from Parent r where r in (select a.parent from Child a where a.attr1=:attr1)";

The problem is that this query can return the same Parent object several times. I need a solution where there is a distinct on Parent, so if there are duplicates, i only get one of those.

I tried "select distinct r from parent r....." but it not works.

Secound, i need the query to be as fast as possible, so if there is a solution that is faster than the above, please give me the details.

Thanks.


How about:

Code:
select distinct p from Parent p inner join p.children child where child.attr=:attr



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 12:45 pm 
Beginner
Beginner

Joined: Sat Jan 15, 2005 4:50 am
Posts: 23
I trid it, and still get duplicate objects. Any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 12:50 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Gabor Beres wrote:
I trid it, and still get duplicate objects. Any idea?


That's weird. What is parent class's mapping? With the distinct you should not get duplicates.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 1:05 pm 
Beginner
Beginner

Joined: Sat Jan 15, 2005 4:50 am
Posts: 23
Could that be the problem, that my Parent class doesn't have an implemented equals()?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 1:11 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Gabor Beres wrote:
Could that be the problem, that my Parent class doesn't have an implemented equals()?


I would assume the check is done on the primary key and in this case it is the database that is doing the distinct part. The generated sql will help a lot. The mapping too.




Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 1:38 pm 
Beginner
Beginner

Joined: Sat Jan 15, 2005 4:50 am
Posts: 23
Ok, they are not duplicate, just has the same data and that misleaded me. Problem solved, thanks.


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