The method "merge" from the EntityManager causes a duplicated "insert"
of a child entity.
-----------------------------------------------------------------------------------------
Key: HHH-5855
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-5855
Project: Hibernate Core
Issue Type: Bug
Components: annotations, core, entity-manager
Affects Versions: 3.6.0, 3.3.1
Environment: Hibernate-Core 3.6.0, Hibernate EntityManager 3.6.0, Derby, Spring
3.0.5 (Configuration), JUnit 4.5
Reporter: Seth Martin
Priority: Critical
Attachments: CascadeMergeTest.zip
The method "merge" from the EntityManager causes a duplicated "insert"
of a child entity.
Related to HHH-3332
h3.Observed Behavior:
When you have an Entity A with a OneToMany(fetch=LAZY, cascade=ALL or (MERGE and PERSIST))
relationship to another Entity B and attempt to add an new B instance to A hibernate will
generate a duplicate insert command upon commit.
I have witnessed this problem occurring when this procedure is followed:
# retrieve an instance of A from persistence (A a = em.find(A.class, id))
# add a new B to A (a.getBs.add(new B()))
# merge instance 'a' back into persistence (em.merge(a))
Upon transaction commit hibernate will generate an additional "insert" statement
and insert the new instance of b into the data source twice.
h3.Expected behavior:
I would expect hibernate to only perform a single insertion when 'a' is merged
back to persistence and the B's are created through the cascade.
h3.Notes (Discoveries):
This problem can be worked around by:
* Changing the OneToMany relationship from a List to a Set
* Only using either Cascade MERGE or PERSIST not both of ALL
* Add a new B to A.getBs by adding to the '0' index (a.getBs.add(0,new B()))
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira