-->
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: parent->child->grandchild results in too many child ob
PostPosted: Mon May 01, 2006 5:08 pm 
Newbie

Joined: Mon Jul 18, 2005 12:45 pm
Posts: 4
I have create a simple set of objects like:

Code:
class Parent{
...
@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
@OnDelete(action=OnDeleteAction.CASCADE)
@Cascade(value={CascadeType.ALL, CascadeType.DELETE_ORPHAN})
public List<Child> getChildren() ..
...
}

class Child {
...
    @ManyToOne()
    @JoinColumn(name="PARENT_ID", nullable=false)
    public Parent getParent()
...
    @OneToMany(fetch=FetchType.EAGER, mappedBy = "configParam")
    @OnDelete(action= OnDeleteAction.CASCADE)
    @Cascade(value={CascadeType.ALL, CascadeType.DELETE_ORPHAN})
    public List<GrandChild> getGrandChildren()

}

class GrandChild {
...
@ManyToOne()
    @JoinColumn(name="CHILD_ID", nullable = false)
    public Child getChild()

}



I then build a structure like:
--parent1
----child1
------grandchild1
------grandchild2
------grandchild3
(so, 1 parent, 1 child, 3 grandchildren)

Saving this structure works fine, and the db is correct. The issue is when I load a parent object it has 3 children instead of 1. Each child has the correct 3 grandchildren, but there should be only 1 child not 3.

The generated SQL looks fine, and there are 3 populated rows as you'd expect. I thought it might be an issue with .equals on the child, but I think it's fine....

Any help is greatly apprecitated!


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 4:49 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
What's this 'configParam'?

@OneToMany(fetch=FetchType.EAGER, mappedBy = "configParam")


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 11:35 am 
Newbie

Joined: Mon Jul 18, 2005 12:45 pm
Posts: 4
That was left over from my real domain model. I changed the names of the objects to parent, child, grandchild in this example. child was named configParam and grandchild was configParamChoice. But it doesn't change the behavior.

@OneToMany(fetch=FetchType.EAGER, mappedBy = "configParam")

should have been:

@OneToMany(fetch=FetchType.EAGER, mappedBy = "child")


BTW, I changed the Parent to have a set of children instead of a list and the problem went away. I assume this is just hiding the issue.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 12:02 pm 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Quote:
BTW, I changed the Parent to have a set of children instead of a list and the problem went away. I assume this is just hiding the issue.


This is expected behaviour, since Set is a set after all :-).

Are you sure you get 3 Child instances?
How is it with List and its index?
Since if the Set merged your Child instances, you could be getting a single instance, but positioning it on the wrong index.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 2:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
do not eager more than one bag semantic connection

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 8:44 pm 
Newbie

Joined: Mon Jul 18, 2005 12:45 pm
Posts: 4
I'm using List semmantics not Bag. Is there some way to get that to work? thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 4:16 am 
Newbie

Joined: Tue May 30, 2006 9:45 am
Posts: 5
I also have exactly the same table's structure parent->child->grandchild and with eager one-to-many associations also get too many grandchild and child objects.. in debug mode I so, that that lists was filled by duplicated (equals by reference) objects, so I just replaced Lists collections with Sets and Maps and problem is gone..


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.