-->
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: Number of objects returned from a query
PostPosted: Wed Apr 12, 2006 12:50 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
I have a query similar to:

from Person p
inner join fetch p.addresses
where status='test'


I'm using the Hibernate Console plugin for eclipse, which generates the sql. I copy the sql and run it on the database and the database returns back two rows.

The only thing that is different b/w the two rows returned is address_id (primary key) and the address_type (discriminator column).

I would expect hibernate to return only one Person object, but instead I get two Person objects which are equal to each other. This seems really strange to me.

1. Am I correct in thinking I should only get back one Person object?
2. If so, then how can I go about debugging/fixing this.

thanks in advance,
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 13, 2006 3:36 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
You'll get duplicate results when you specify fetch on a join for a collection because the Hibernate result count always matches the JDBC row count. You can removed the duplicates by inserting them into a set.

http://www.hibernate.org/117.html#A11


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 13, 2006 10:54 am 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
ok I found a solution that is more the hibernate way of doing things. Creating a set to solve this problem just seems like it is just darn wrong.

Criteria criteria = session.createCriteria(Person.class);
results = criteria.setFetchMode("addresses", FetchMode.JOIN)
.add(Expression.eq("status", "test"))
.setResultTransformer(CriteriaSpecification.DISTIN CT_ROOT_ENTITY)
.list();

This will return only one Person object, which is exactly what I want. Now the question is...why can't you do this in HQL. Or Rather...how do you do it in HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 13, 2006 11:57 am 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
According to the following link, it looks as if hibernate 3.2 will have transformers available to the query object. Hence, the equivalent could be done with HQL.

http://blog.hibernate.org/cgi-bin/blosx ... d_sql.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 14, 2006 4:58 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
mmasters - if you look at the code for the DistinctRootEntityResultTransformer you will see that all that does is use a HashSet to create a new list internally.


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.