users@glassfish.java.net

Persist/Merge entities

From: <glassfish_at_javadesktop.org>
Date: Fri, 25 May 2007 11:45:31 PDT

Hi,
I have the following entities:

public class Parent {
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.All)
private Collection<Child> childs;
...
}

public class Child {
@JoinColumn(name="PARENT_ID", nullable=true, unique=false)
@ManyToOne(optional=false)
private Parent parent;
...
}



In a stateful bean, with a method such as:

public void updateParent(Parent parent) {
  em.merge(parent);
}

And with another method that adds a child to parent such as:

public void addChildsToParent(Parent parent) {
  Child child = new Child();
  child.setParent(parent);
  parent.getChilds().add(child);
}


This worked fine and I could see at the end of the transaction that another row added in childs table.

Now the problems comes when I'm trying to update the childs via GUI by the following scenario:

1) I create a dataTable that display childs as:
<h:dataTable value="#{parent.childs"} var="child">
...
...<inputText per child's field>...
</h:dataTable>

and after the childs are modified in the web, I call 'updateParent(parent)'

Then here comes the problem:
Assuming I had '3' childs loaded with the Parent. when I call 'updateParent' after modifying them, they all gets modified. but if I add other childs with 'addChildsToParent', after these childs were persisted (by updateParent), modifying them and calling 'updateParent' again does not affect these rows in the DB, they just stay as is.


So calling 'updateParent', I only see the childs that were 'LOADED' with the parent get updated, but new childs that were just persisted are not.

What could be the reason for that?

Thanks.
[Message sent by forum member 'demiant' (demiant)]

http://forums.java.net/jive/thread.jspa?messageID=218985