[ 
https://issues.apache.org/jira/browse/OPENJPA-1787?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13005251#comment-13005251
 ] 

Jeremy Bauer commented on OPENJPA-1787:
---------------------------------------

I tracked down the source of the problem.  When merging a new entity, OpenJPA 
persists a new "empty" entity, sets (or retrieves) an ID for the new entity, 
and then merges in the object passed in.  The problem is that the during the 
persist of the empty entity, a PRE_PERSIST lifecycle event gets fired, causing 
validation to occur.  I've found that not only is validation occurring 
prematurely, but OpenJPA violates the JPA specification for the PrePersist 
callback as well.  

<jpa 1.0 & 2.0 spec - 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.
<jpa 1.0 & 2.0 spec - section 3.5.2/>

By adding this simple callback to the Person class, it showed that a PrePersist 
callback occurred before the entity was fully populated. 
class Person {
...

    @PrePersist
    public void prePersist() {
        System.out.println("PrePersist getName()=" + this.getName());
    }
}

Result:  PrePersist getName()=null

I think the solution will be to disable this callback for the case where a new 
entity is created specifically for the purposes of a merge.  Working on a fix...

> Bean validation fails merging a new entity
> ------------------------------------------
>
>                 Key: OPENJPA-1787
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-1787
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa, kernel
>    Affects Versions: 2.0.1, 2.1.0, 2.2.0
>            Reporter: Oliver Ringel
>            Assignee: Jeremy Bauer
>            Priority: Critical
>         Attachments: com.example.TestEmployeeDAO.txt, openjpa-1787.tar, 
> testcase-openjpa-1787.tar
>
>
> The bean validation is not working correctly
> If you try to merge a new entity.
>         EntityManager em = entityManagerFactory.createEntityManager();
>         Person person = new Person();
>         person.setName("Oliver");                               // 
> Employee.name is annotated @NotNull 
>         person = em.merge(person);                            
> you get a ConstraintValidationException, although name is set. 

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to