users@glassfish.java.net

Re: _at_EntityListener and merge() not working well

From: <glassfish_at_javadesktop.org>
Date: Wed, 30 Jan 2008 14:21:33 PST

Please file a bug for this issue. The problem is that TopLink is invoking the PrePersist on the managed object before merging from the detached object into the newly persisted object. The update succeeds on the PrePersist but then TopLink removes the updates as the detached object is merged into the managed copy.

To work around this issue you could register for the TopLink postMerge event through a SessionCustomizer.

Persistence.xml entry:
<property name="toplink.session.customizer" value="mypackage.EventCustomizer"/>

Customizer Class :
package mypackage;
import oracle.toplink.essentials.sessions.Session;
import oracle.toplink.essentials.mappings.DirectCollectionMapping;
import oracle.toplink.descriptors.RelationalDescriptor;

/**
* PUBLIC:
* This interface is to allow extra customization on a TopLink Session
*/
public class EventCustomizer extends SessionCustomizer {
 public void customize(Session session) throws Exception {
   //get internal representation of Class to Database mappings
   RelationalDescriptor descriptor = (RelationalDescriptor)session.getDescriptor(Employee.class);
   //register for the post merge event
   DescriptorEventListener listener = new DescriptorEventAdapter(){
       public void postMerge(DescriptorEvent event) {
            ((Employee)event.getObject()).prePersistMethod();
        }
    };
    descriptor.getEventManager().addListener(listener);
}

--Gordon
[Message sent by forum member 'gyorke' (gyorke)]

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