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

Jeremy Bauer commented on OPENJPA-722:
--------------------------------------

I conversed with a couple of the committers and the consensus was that we 
should not be storing persistence context-type state in the detached entities.  
Instead, the detached entity should only manage information about the state of 
its persistent fields.

So, when we re-merge entities we should not depend on the "NEW" state in a 
detached object.  The entity could have been inserted into the database outside 
of the application, in which case we'd fail trying to do an insert (which 
should not happen on a merge).  I think the ideal solution is to re-query for 
the entity and then perform the insert or update based on whether the entity 
was or was not found - similar to merging an actual new entity.

One additional item:  The test code has a query immediately following the clear:

        em.clear();
        assertEquals(1, query("select x from Item x "
                + "where x.itemName = 'cup'").
                    getResultList().size());

This essentially loads the returned item in the L1 cache, changing the behavior 
of the internal merge processing   (the corresponding merge finds the item in 
the L1).  While this is a good scenario to test, we should also test the 
scenario where the item isn't cached before the merge.


> persist - clear - merge scenario doesn't work
> ---------------------------------------------
>
>                 Key: OPENJPA-722
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-722
>             Project: OpenJPA
>          Issue Type: Sub-task
>          Components: kernel
>    Affects Versions: 1.0.3, 1.1.0, 1.2.0
>            Reporter: Xiaoqin Feng
>            Assignee: Jeremy Bauer
>         Attachments: openjpa-722-fromQin.patch, 
> TestEntityManagerClear-V2.java, TestEntityManagerClear.java
>
>
> EntityManager.clear() now don't  flush new object but only detach it.
> But DetachManager still flush dirty object and assume detached objects are in 
> clean state.
> When the "new" object is merged back and transaction commit,  because the 
> object state lost its original state PNEW, it will not be added to insert 
> list  and not flushed to DB.
> According to the EntityManager.clear() API,  changes made to entities that 
> have not been flushed to the database will not be persisted.  When they 
> merges back to persistent context, they all should kept there original state.
> I added the following test to 
> org.apache.openjpa.persistence.simple.TestEntityManagerClear.
>     public void testClearMerge() {
>         // Create EntityManager and Start a transaction (1)
>         begin();
>         // Insert a new object then clear persistent context
>         AllFieldTypes testObject1 = new AllFieldTypes();
>         testObject1.setStringField("my test object1");
>         persist(testObject1);
>         //Object1 is not flushed to DB but only detached by clear().
>         em.clear();
>         em.merge(testObject1);
>         //expect the PCState is same as before detached, 
>         //so it is PNew instead of PCLEAN and is add to insert list.
>         commit();
>         
>         //Start a new transaction
>         begin();
>         
>         // Attempt retrieve of Object1 from previous PC (should exist)
>         assertEquals(1, query("select x from AllFieldTypes x "
>             + "where x.stringField = 'my test object1'").
>                 getResultList().size());
>         
>         // Rollback the transaction and close everything
>         rollback();
>     }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to