-->
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: OneToMany fetchType EAGER produces duplicate results
PostPosted: Mon Nov 07, 2005 5:43 pm 
Newbie

Joined: Sun May 15, 2005 12:01 am
Posts: 8
Hibernate version:

hibernate-3.1rc2
hibernate-annotations-3.1beta6


I seem to get different results with LAZY vs. EAGER fetching on this relationship. The two entities involved are Group and Resource with a bidirectonal OneToMany / ManyToOne

Here's the relevant beans / annotations

Code:
public class ResourceBean {
...
    @ManyToOne( fetch=FetchType.EAGER )
    private GroupBean group;
}

public class GroupBean {
...
    @OneToMany( mappedBy="group", fetch=***FetchType.EAGER***)
    private Set<ResourceBean> resources = new HashSet<ResourceBean>();
}


If the highlighted fetchType is EAGER, and I do a 'findAll' on Group, I get duplicate entries from the Resources join. If the fetch type is LAZY, the results are as I'd expect (one object per Group).

I'm using the code patterns in CaveatEmptor demo, so the findall is....


Code:
   public List<T> findAll() {
        return findByCriteria();
    }
    protected List<T> findByCriteria(Criterion... criterion) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        for (Criterion c : criterion) {
            crit.add(c);
        }
        return crit.list();
    }



The Eager SQL is basically...
Code:
select * from groups this_
left outer join resources resources3_ on this_.groupId=resources3_.group_groupId


Am I doing something wrong or is this a bug? Is it my responsibility to somehow unique in the findByCriteria? (eg. I've seen posts about putting results in a Set to remove duplicates). Just doesn't seem right to have to do that here, so I figured I'd post...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 7:43 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you're using java.util.List with no indexed column so it is considered a bag hence fetching 2 collections including a bag is not possible in hibernate

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 9:37 am 
Newbie

Joined: Tue Dec 13, 2005 9:13 am
Posts: 4
I have the same problem when i eagerly fetch an object with, in this case, a couple of List-collections. And while I understand why this happens with an outer join i tried with setting the FetchMode to FetchMode.SELECT in a query
and it correctly fetches the correct number of beans in the collection.
However, I have a generic dao, and to set this on the load operation for every possible bean and its collections is not feasible.
I'd thought i'd set the fetchmode in the mapping file but since i use annotations i'd like to annotate the bean. But i can't find a way to do that.
Please make it possible!
I'll go down another path till then :-)
/björn


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 12:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
this is already possible use fetch=FetchType.LAZY on you associations

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 12:28 pm 
Newbie

Joined: Tue Dec 13, 2005 9:13 am
Posts: 4
oh i thought i could fetch eagerly with select fetchmode.
i didnt know that setting fetchmode.select implicitly made my fetch lazy.
i used a Set object instead of List though.


Top
 Profile  
 
 Post subject: Mapping is for a set - still need an answer to this
PostPosted: Fri Mar 07, 2008 12:18 am 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
Emmanuel, I think you mis-read his post. The findAll method returns a list, but the mapping is a Set, not a List. His mappings have no bags.

Can you clarify why you say it is a problem with bags?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 08, 2008 1:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
ah, to the original problem, the query needs to use a DistinctRootEntityResultTransformer

_________________
Emmanuel


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.