EntityType.deepCopy needs to copy for EntityType.DOM4J
Description
Activity
Brett MeyerDecember 16, 2013 at 9:36 PM
Out of date – DOM4J entity mode no longer supported
Former userSeptember 29, 2010 at 8:56 PM
Looks like the issue is due to the bidirectional relationship between DOM parent and child elements. For POJOs, this isn't usually an issue since the component "value" is just also set as a property on the copied parent class, without being removed from the parent in the query results (technically violating composition rules, but the state of these copies is all we really care about). For all modes, property values are simply returned rather than copied, but in the case of DOM (and I suspect also Map of Maps) adding the value to the copied parent also detaches that value from its previous parent, thus "stealing" it from the actual results. The best way to address this is probably via entity-mode specific code for a deepCopy method introduced to the tuplizer that would actually create a copy of the value, but since that would introduce a breaking change to the tuplizer spi, this is being re-targeted for 4.x.
Former userSeptember 27, 2010 at 4:46 PM
I guess to clarify my previous comment, I was looking at potentially doing another deep copy of the value via the tupilzer, but that would require access to the session as well as the tupilizer itself.
Former userSeptember 27, 2010 at 4:43 PM
I've determined the problem, but it looks like the solution might be a bit of a mess. The problem is when creating a copy of query results to store in the entityEntry, the portion of the code that deals with copying the property value doesn't take into consideration when that value is yet another entity, and ends up moving it from the results to be returned to the user to the copy, leaving the user with null where an entity reference should have been. It seems like the solution should involve using the appropriate entity-mode-specific tupilizer in the deepCopy method for the property type class, but I can't see a good way of making that available without changing the API fairly significantly or doing something hackish like re-creating a parent component's metamodel object.
Using DOM4J with a set of composite-elements that contains a many-to-one. When loading this from the database, the many-to-one piece of the composite-element is disappearing from the XML. I can see the collection being built and the properties on the elements of the collection being set, but the many-to-one property disappears after that.
Investigating this, it looks like when PersistentElementHolder.getSnapshot is called and a deepCopy is performed, the EntityType.deepCopy method returns the value to be copied rather than copying it. This interacts poorly with the DOM4J tree, since each Element can only have a single Element parent. When the properties are set on this, a detach is performed that yanks the original element out of its parent.