-->
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: object with joined collection is in return list many times
PostPosted: Wed Jul 12, 2006 4:36 pm 
Newbie

Joined: Tue Jul 11, 2006 4:22 pm
Posts: 9
Hello

I have a two objects Doctor and Contact to doctor.
Doctor can have more than one Contact.


i have select like this

select doctor from Doctor doctor left join fetch doctor.contactdoctorSet where doctor.specialization in (:specialization)

When doctor1 have three contacts it returns three rows with the same doctor1 like this, than other doctors with one contact only once:
[doctor1]
[doctor1]
[doctor2]
[doctor3]
...
[doctorN]


is any way how it can return collection of contacts as one record for doctor with already initialized collection

[doctor1]
[doctor2]
[doctor3]
...
[doctorN]


Maybe i can use uniqueresult but isn't it very slow ?

Can any1 help me ? plz

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 5:22 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
one of the way would be using criteria
with

criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

The equivalent in HQL is to use select distinct:

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 3:45 am 
Newbie

Joined: Tue Jul 11, 2006 4:22 pm
Posts: 9
thanks that is what i was asking for. Is any other way how to get unique result and do not use distinct. Distinct is very slow isn't it ?

what is faster :

using query.uniqueResult()

or

using your proposal with

criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

or any other way ... ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 7:17 am 
Beginner
Beginner

Joined: Tue May 23, 2006 4:10 pm
Posts: 38
Location: Charleston, SC
I had the very same problem and have found a couple of ways to solve it. If you expect the result to be unique, use uniqueResult. I have personally observed distinct return dups when I joined more than two tables; it worked well when I joined two tables. The best solution seems to be code like the following:

List resultsWithDups = query.list();
Set resultSet = new HashSet(resultsWithDups);

The set doesn't allows dups so it effectively filters the list using the object's equals method.

Grant


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.