cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r515663 - in /cayenne/main/trunk/itests/jpa-chapter3/src: main/java/org/apache/cayenne/jpa/itest/ch3/entity/CallbackEntity.java test/java/org/apache/cayenne/jpa/itest/ch3/_3_5_1_LifecycleCallbackMethodsTest.java
Date Wed, 07 Mar 2007 17:56:25 GMT
Author: aadamchik
Date: Wed Mar  7 09:56:24 2007
New Revision: 515663

URL: http://svn.apache.org/viewvc?view=rev&rev=515663
Log:
Attempting to reproduce CAY-766 in JPA environment - it works! (since temp id is not exposed
to the code)

Modified:
    cayenne/main/trunk/itests/jpa-chapter3/src/main/java/org/apache/cayenne/jpa/itest/ch3/entity/CallbackEntity.java
    cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_5_1_LifecycleCallbackMethodsTest.java

Modified: cayenne/main/trunk/itests/jpa-chapter3/src/main/java/org/apache/cayenne/jpa/itest/ch3/entity/CallbackEntity.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter3/src/main/java/org/apache/cayenne/jpa/itest/ch3/entity/CallbackEntity.java?view=diff&rev=515663&r1=515662&r2=515663
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter3/src/main/java/org/apache/cayenne/jpa/itest/ch3/entity/CallbackEntity.java
(original)
+++ cayenne/main/trunk/itests/jpa-chapter3/src/main/java/org/apache/cayenne/jpa/itest/ch3/entity/CallbackEntity.java
Wed Mar  7 09:56:24 2007
@@ -40,6 +40,9 @@
 
     @Transient
     protected transient boolean postPersistCalled;
+    
+    @Transient
+    protected transient int postPersistedId;
 
     @Transient
     protected transient boolean preRemoveCalled;
@@ -64,6 +67,7 @@
     @PostPersist
     public void postPersistMethod() {
         postPersistCalled = true;
+        postPersistedId = id;
     }
 
     @PreRemove
@@ -101,6 +105,10 @@
 
     public boolean isPostPersistCalled() {
         return postPersistCalled;
+    }
+    
+    public int getPostPersistedId() {
+        return postPersistedId;
     }
 
     public boolean isPostRemoveCalled() {

Modified: cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_5_1_LifecycleCallbackMethodsTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_5_1_LifecycleCallbackMethodsTest.java?view=diff&rev=515663&r1=515662&r2=515663
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_5_1_LifecycleCallbackMethodsTest.java
(original)
+++ cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_5_1_LifecycleCallbackMethodsTest.java
Wed Mar  7 09:56:24 2007
@@ -21,6 +21,7 @@
 import javax.persistence.EntityManager;
 
 import org.apache.cayenne.itest.jpa.EntityManagerCase;
+import org.apache.cayenne.itest.jpa.ItestSetup;
 import org.apache.cayenne.jpa.itest.ch3.entity.CallbackEntity;
 import org.apache.cayenne.jpa.itest.ch3.entity.CallbackEntity2;
 import org.apache.cayenne.jpa.itest.ch3.entity.EntityListener1;
@@ -71,5 +72,49 @@
                 + ":"
                 + EntityListener1.class.getName(), EntityListenerState
                 .getPrePersistCalled());
+    }
+
+    public void testPostPersist() {
+
+        // regular entity
+        CallbackEntity e = new CallbackEntity();
+        assertFalse(e.isPostPersistCalled());
+
+        // don't use super getEntityManager - it starts a tran
+        EntityManager em = ItestSetup.getInstance().createEntityManager();
+
+        // spec reqires the callback to be invoked as a part of persist, without waiting
+        // for flush or commit.
+        em.getTransaction().begin();
+        em.persist(e);
+        assertFalse(e.isPostPersistCalled());
+        assertEquals(0, e.getPostPersistedId());
+        em.getTransaction().commit();
+
+        assertTrue(e.isPostPersistCalled());
+        
+        // Per spec, id must be availble during PostPersist
+        assertEquals(e.getId(), e.getPostPersistedId());
+
+        // external listeners
+        EntityListenerState.reset();
+        assertEquals("", EntityListenerState.getPostPersistCalled());
+        ListenerEntity1 e3 = new ListenerEntity1();
+
+        // reset EM
+        em = ItestSetup.getInstance().createEntityManager();
+        assertEquals("", EntityListenerState.getPostPersistCalled());
+        em.getTransaction().begin();
+        em.persist(e3);
+        assertEquals(":" + EntityListener2.class.getName(), EntityListenerState
+                .getPostPersistCalled(), EntityListenerState.getPostPersistCalled());
+        EntityListenerState.reset();
+        
+        em.getTransaction().commit();
+        assertEquals(":"
+                + EntityListener1.class.getName()
+                + ":"
+                + EntityListener2.class.getName(), EntityListenerState
+                .getPostPersistCalled());
     }
 }



Mime
View raw message