-->
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.  [ 2 posts ] 
Author Message
 Post subject: Fetch mode JOIN returns duplicate results
PostPosted: Tue Jun 06, 2006 7:44 pm 
Newbie

Joined: Thu Apr 08, 2004 8:46 am
Posts: 11
Location: Istanbul
Hibernate version:
3.1.3
Mapping documents:
Code:
    <class name="Ihtiyac" table="ihtiyac">
        <id name="no" unsaved-value="-1" ><generator class="hilo" >
            <param name="max_lo">1</param>
            <param name="table">key_ihtiyac</param>
        </generator></id>
        <property name="adet"/>
        <many-to-one class="Durak" name="durak"/>
        <set name="karsilayanlar" table="ihtiyac_karsilayanlar" lazy="true">
            <key column="ihtiyac" not-null="true"/>
            <composite-element class="AracYol" node="aracyol">
                <many-to-one name="arac" column="arac"/>
                <many-to-one name="yol" column="yol"/>
            </composite-element>
        </set>
   </class>

Code between sessionFactory.openSession() and session.close():

Code:
            public Object doInHibernate(Session session) throws HibernateException,SQLException {
                return session.createCriteria(Ihtiyac.class)
                .setFetchMode("karsilayanlar",FetchMode.JOIN)
                .list();
            }


Full stack trace of any exception that occurs:

Name and version of the database you are using:
Sybase Adaptive Server Anywhere 9.0.1

The generated SQL (show_sql=true):

Code:
select this_.no as no9_0_, this_.adet as adet9_0_, this_.durak as durak9_0_, karsilayan2_.ihtiyac as ihtiyac2_, karsilayan2_.arac as arac2_, karsilayan2_.yol as yol2_ from ihtiyac this_ left outer join ihtiyac_karsilayanlar karsilayan2_ on this_.no=karsilayan2_.ihtiyac


Debug level Hibernate log excerpt:


Hi,
I have the mapping and criteria query above. The generated SQL is ok, no exceptions etc. When I eager fetch the query as above, I receive duplicate results. I expect ro receive same number of results as I issued the query with FetchMode.DEFAULT (or w/o an explicit FetchMode). However, I receive as many results as the cartesian product of the two tables. I must be missing something.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:51 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
With FetchMode.JOIN and a one-to-many or many-to-many, you need to filter out the dupes. With a criteria query, you can do this via a result transformer:

Code:
public Collection doInHibernate(Session session) throws HibernateException,SQLException {
                return session.createCriteria(Ihtiyac.class)
                .setFetchMode("karsilayanlar",FetchMode.JOIN)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                .list();
            }


Adding that one extra line in there will do the trick. HQL Queries support result transformers as of Hibernate 3.2. An alternatitive wold be passing your results to a new HashSet:

Code:
public Collection doInHibernate(Session session) throws HibernateException,SQLException {
         List results =  session.createCriteria(Ihtiyac.class)
                .setFetchMode("karsilayanlar",FetchMode.JOIN)
                .list();
         return new HashSet(results);
            }





Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


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