-->
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: Duplicate entries in criteria.list but not in query.iterate
PostPosted: Wed Jun 29, 2005 5:11 pm 
Newbie

Joined: Wed Jun 29, 2005 4:36 pm
Posts: 4
I joined class A with B using left outer join where B is a Set in A. The SQL returns a result set like

A.x A.y B.x B.z
---------------------
1 5 1 1
1 5 1 2

I expect 1 instance of A with a Set of 2 instances of B.
Session.get() method returns correctly with 1 instance of A.
Query.iterate() method returns correctly.

But
Criteria criteria = session.createCriteria(A.class);
criteria.add(Restrictions.eq("x", new Long(1)));
criteria.list(); returns 2 instances of A with a Set of 2 instances of B.

Interestingly, when I debug the List which has 2 elements of A, only the element I inspect first (order doesn't matter) shows all instance variables including the Set of B, and the other element is empty.

Does this mean Hibernate creates only one object and creates a List with 2 references to the same object?

Here is the question I found in FAQ

Using join fetch returns duplicate parent, is it expected ? Answer: Yes.

If so, why is it inconsistant beween get(), iterator() and list()?

[b]Hibernate version:[/b] 3.0

[b]Mapping documents:[/b]
<class name="A" table="A" mutable="false">
<id name="x" type="long" >
<column name="x" not-null="true"/>
<generator class="native" />
</id>

<property name="y" type="int">
<column name="y" not-null="true"/>
</property>

<set name="b" outer-join="true">
<key column="x" />
<one-to-many class="B" />
</set>
</class>

[b]Code between sessionFactory.openSession() and session.close():[/b]

[b]Full stack trace of any exception that occurs:[/b]

[b]Name and version of the database you are using:[/b]
DB2 UDB for AS/400
05.03.0000 V5R3m0
AS/400 Toolbox for Java JDBC Driver
5.0

[b]The generated SQL (show_sql=true):[/b]
select a.x, a.y, b.x, b.z from A a left outer join B b on a.x = b.x where a.x = ?

[b]Debug level Hibernate log excerpt:[/b]


Top
 Profile  
 
 Post subject: Duplicate entries in criteria.list but not in query.iterate
PostPosted: Thu Jun 30, 2005 2:43 pm 
Newbie

Joined: Thu May 26, 2005 1:04 pm
Posts: 13
You may wanto use Projects or the one which I use is

Code:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

instead of criteria.list();

Code:
Criteria criteria = session.createCriteria(A.class);
criteria.add(Restrictions.eq("x", new Long(1)));
<snip>
criteria.list();
</snip>
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

_________________
RT


Top
 Profile  
 
 Post subject: Re: Duplicate entries in criteria.list but not in query.iter
PostPosted: Thu Jun 30, 2005 2:44 pm 
Newbie

Joined: Thu May 26, 2005 1:04 pm
Posts: 13
rtarar wrote:
You may wanto use Projects or the one which I use is

Code:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

instead of criteria.list();

Code:
Criteria criteria = session.createCriteria(A.class);
criteria.add(Restrictions.eq("x", new Long(1)));
<snip>
criteria.list();
</snip>
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();


Please read Projects as Projections
-sorry for typo

_________________
RT


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 2:52 pm 
Newbie

Joined: Wed Jun 29, 2005 4:36 pm
Posts: 4
Thanks. setResultTransformer works.


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.