-->
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.  [ 3 posts ] 
Author Message
 Post subject: Merging managed parent with new child creates two children.
PostPosted: Mon Jul 16, 2007 2:36 pm 
Newbie

Joined: Mon Jul 16, 2007 2:12 pm
Posts: 1
Using:

spring-2.0.6
hibernate-3.2.4.sp1
hibernate-annotations-3.3.0.ga
hibernate-commons-annotations-3.3.0.ga
hibernate-entitymanager-3.3.1.ga
hibernate-validator-3.0.0.ga


Issue:

I am using spring/jpa/hibernate for persistence and I am having issues with a very simple scenario. I have two domain objects. Let's call them Parent and Child:

Code:
@Entity
public class Parent
{
    private Long id;
    private List<Child> children;

    public Parent()
    {
        children = new ArrayList<Child>();
    }

    @Id
    @GeneratedValue
    public Long getId()
    {
        return this.id;
    }

    private void setId(Long id)
    {
        this.id = id;
    }

    @OneToMany(mappedBy = "parent", cascade = {CascadeType.ALL})
    public List<Child> getChildren()
    {
        return children;
    }

    public void setChildren(List<Child> children)
    {
        this.children = children;
    }
}

@Entity
public class Child
{
    private Long id;
    private Parent parent;

    @Id
    @GeneratedValue
    public Long getId()
    {
        return this.id;
    }

    private void setId(Long id)
    {
        this.id = id;
    }

    @ManyToOne
    public Parent getParent()
    {
        return parent;
    }

    public void setParent(Parent parent)
    {
        this.parent = parent;
    }
}


I have a service method wrapped in a transaction in which I get a parent and add a child to it:
Code:
    Parent parent = parentDAO.get(id) // calls entityManager.find(Parent.class, id);
    Child child  = new Child();
    child.setParent(parent);
    parent.getChildren().add(child);
    parentDAO.save(parent) // calls entityManager.merge(parent)


Right after calling save, the parent has two copies of the same child that I added. This causes two children to be inserted to the database (each with their own id). If I don't call save, it appears to work ok, but in real world usage, the parent that I get from the application may be detached.

Does anyone out there have any knowledge about why this would be happening and how I can fix the problem?

Thanks!

Will


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 07, 2007 2:03 pm 
Newbie

Joined: Thu Jan 27, 2005 12:26 pm
Posts: 5
I'm having the same problem. Did you ever figure it out?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 07, 2007 2:54 pm 
Beginner
Beginner

Joined: Thu Jun 17, 2004 11:25 pm
Posts: 21
Location: Los Angeles
I'm not positive, but I'm pretty sure the problem is that you are calling merge() on an attached object (one that you fetched in the current session). You normally want to call save() or saveOrUpdate() in this case.

There is also a problem in that you have mappedBy on the wrong side of the association. It looks like you want new children to be automatically persisted when added to a parent, but you've told Hibernate (via the mappedBy attribute) to update the parent/child association when the parent is added to the child. Try moving the mappedBy attribute to the @ManyToOne annotation.

I think what happens here is that child.setParent() effectively adds the child to the parent in the database and then the merge() operation merges (adds) another child from the parent side.


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