Merging Parent entity with a Lazy Child Collection having orphanRemoval=true
will delete 'not-fetched' children
---------------------------------------------------------------------------------------------------------------
Key: OPENJPA-2028
URL: https://issues.apache.org/jira/browse/OPENJPA-2028
Project: OpenJPA
Issue Type: Bug
Components: jpa
Affects Versions: 2.1.0, 2.1.1
Environment: Win XP3, Java 1.6.0_25, PostgreSQL 9.04, JDBC jar:
postgresql-9.0-801.jdbc4
OpenJPA 2.1.0 or 2.1.1-20110610.205956-18-binary
Reporter: Jim O'Rourke
Given the following entity declarations:
@Entity
public class Parent{
@Column
private String name;
public void setName(String nm){this.name=nm;}
@OneToMany(mappedBy="parent", fetch=LAZY, cascade = ALL, orphanRemoval=true)
private Set<Child> children;
public Set<Child> getChildren(){return children;}
}
@Entity
public class Child{
@ManyToOne(optional = false, targetEntity = Parent.class)
@JoinColumn(name="parent_id", nullable=false)
private Parent parent;
}
Both entities have @Id and @Version properties declared and implement
appropriate hashCode, equals and compareTo methods.
The following code will erroneously delete a parent's children from the
datastore when the persistence context does not have a reference to p or at
least has not already fetched p.children:
Parent p = em.find(Parent.class, someID);
//PersistenceUnitUtil.isLoaded(p) returns true
//PersistenceUnitUtil.isLoaded(p, "children") returns false
p.setName("Something");
em.getTransaction().begin();
Parent result=em.merge(p);
em.getTransaction().commit();
Workarounds: FetchType.EAGER or p.getChildren() before merge(p)
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira