Bug 435044 - Prepersist ISSUE with merge(when creating a new entity).
Summary: Prepersist ISSUE with merge(when creating a new entity).
Status: REOPENED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P3 critical with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords: performance
Depends on:
Blocks:
 
Reported: 2014-05-16 07:15 EDT by Dan Cebotarenco CLA
Modified: 2022-06-09 10:26 EDT (History)
2 users (show)

See Also:


Attachments
Test Case EclipseLink (7.56 MB, application/octet-stream)
2014-05-16 07:15 EDT, Dan Cebotarenco CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Dan Cebotarenco CLA 2014-05-16 07:15:21 EDT
Created attachment 243167 [details]
Test Case EclipseLink

We want to create a new entity. We call merge on entity , @Prepersist is called but changed are not applied to entity.
Please Note:
* When i call persist on entity prepersist is called and changes are applied!

Related to this i can make such conclusion:
 - prepersist works when i call a persist on entity
 - preupdate works when i call a merge on entity
 - prepersist DOESN'T work when i call merge on a new entity!!!

Below i provided a test Case application. Test app.
Comment 1 Chris Delahunt CLA 2014-05-16 12:26:29 EDT
This is not a bug.  The prepersist is called on the managed entity, which has the changes made to it, and then the merge is applied.  The problem with your test case is that you are modifying a relationship in the prepersist.  The merge will cause this change to be overriden with null.  

This works differently in the other cases:
When calling persist, the entity passed in is used for the prepersist, so the persist will pick up the change.  
When using merge and preupdate, the preUpdate happens after the merge.  PreUpdate is tied to the database operation, not the JPA merge operation.
Comment 2 Chris Delahunt CLA 2014-05-16 20:42:50 EDT
The specification states the in section 3.5.2 
"For entities to which the merge operation has been applied and causes the creation of newly managed instances, the PrePersist callback methods will be invoked for the managed instance after the entity state has been copied to it."

This should be expected to work, and I am reopening for investigation.
Comment 3 Dan Cebotarenco CLA 2014-05-27 05:26:20 EDT
What is the status of the issue? could i recieve a patch?
Comment 4 Clovis Wichoski CLA 2015-02-07 07:11:16 EST
For me this problem occurs too, I have an entity with a Listener for with an @PreUpdate annoted method, the @PreUpdate method never is called, the workaround to avoid the problem is to call the listener method before calling merge.

I'm using Tomcat and Jersey with a REST API and EclipseLink as the JPA engine.

here the snippets:

1) from REST call
    @POST
    @Path("{id}")
    @Consumes({ "application/xml", "application/json"})
    public void edit(@PathParam("id") Integer id, Usuario entity) {
        //TODO: waiting solution of BUG https://bugs.eclipse.org/bugs/show_bug.cgi?id=435044
        UsuarioListener usuarioListener = new UsuarioListener();
        usuarioListener.atualizaDados(entity);
        super.edit(entity);
    }

2) The super implementation edit method
    public void edit(T entity) {
        em.getTransaction().begin();
        ((Persistable) entity).refreshReferences(em);
        em.merge(entity);
        em.getTransaction().commit();
    }
Comment 5 Eclipse Webmaster CLA 2022-06-09 10:26:51 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink