Author: hardy.ferentschik
Date: 2008-06-29 09:51:08 -0400 (Sun, 29 Jun 2008)
New Revision: 14825
Modified:
entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java
entitymanager/trunk/src/java/org/hibernate/ejb/event/EJB3PostUpdateEventListener.java
entitymanager/trunk/src/test/org/hibernate/ejb/test/callbacks/CallbacksTest.java
Log:
EJB-288
* Added PostCollectionRecreateEventListener and test case. Not sure about the other
PostCollection*EventListener. Need to investigate when and where they fire and if we have
to add them as well.
Modified: entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java
===================================================================
---
entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java 2008-06-29
13:47:11 UTC (rev 14824)
+++
entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java 2008-06-29
13:51:08 UTC (rev 14825)
@@ -37,6 +37,7 @@
import org.hibernate.event.FlushEventListener;
import org.hibernate.event.MergeEventListener;
import org.hibernate.event.PersistEventListener;
+import org.hibernate.event.PostCollectionRecreateEventListener;
import org.hibernate.event.PostDeleteEventListener;
import org.hibernate.event.PostInsertEventListener;
import org.hibernate.event.PostLoadEventListener;
@@ -46,8 +47,8 @@
import org.hibernate.event.PreLoadEventListener;
import org.hibernate.event.PreUpdateEventListener;
import org.hibernate.event.SaveOrUpdateEventListener;
+import org.hibernate.event.def.DefaultPostLoadEventListener;
import org.hibernate.event.def.DefaultPreLoadEventListener;
-import org.hibernate.event.def.DefaultPostLoadEventListener;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.secure.JACCPreDeleteEventListener;
import org.hibernate.secure.JACCPreInsertEventListener;
@@ -127,9 +128,13 @@
listenerConfig.setPostLoadEventListeners(
new PostLoadEventListener[] { new EJB3PostLoadEventListener(), new
DefaultPostLoadEventListener() }
);
+
+ EJB3PostUpdateEventListener postUpdateEventListener = new
EJB3PostUpdateEventListener();
listenerConfig.setPostUpdateEventListeners(
- new PostUpdateEventListener[] { new EJB3PostUpdateEventListener() }
+ new PostUpdateEventListener[] { postUpdateEventListener }
);
+ listenerConfig.setPostCollectionRecreateEventListeners(
+ new PostCollectionRecreateEventListener[] { postUpdateEventListener });
}
public void setProperties(Properties properties) {
Property changes on:
entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java
___________________________________________________________________
Name: svn:keywords
- Author Date Id Revision
+ Id
Modified:
entitymanager/trunk/src/java/org/hibernate/ejb/event/EJB3PostUpdateEventListener.java
===================================================================
---
entitymanager/trunk/src/java/org/hibernate/ejb/event/EJB3PostUpdateEventListener.java 2008-06-29
13:47:11 UTC (rev 14824)
+++
entitymanager/trunk/src/java/org/hibernate/ejb/event/EJB3PostUpdateEventListener.java 2008-06-29
13:51:08 UTC (rev 14825)
@@ -1,3 +1,4 @@
+// $Id:$
/*
* JBoss, the OpenSource EJB server
*
@@ -6,16 +7,21 @@
*/
package org.hibernate.ejb.event;
+import org.hibernate.engine.EntityEntry;
+import org.hibernate.engine.Status;
+import org.hibernate.event.EventSource;
+import org.hibernate.event.PostCollectionRecreateEvent;
+import org.hibernate.event.PostCollectionRecreateEventListener;
import org.hibernate.event.PostUpdateEvent;
import org.hibernate.event.PostUpdateEventListener;
-import org.hibernate.engine.EntityEntry;
-import org.hibernate.engine.Status;
/**
* @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
- * @version $Revision$
+ * @version $Revision: 11282 $
*/
-public class EJB3PostUpdateEventListener implements PostUpdateEventListener,
CallbackHandlerConsumer {
+@SuppressWarnings("serial")
+public class EJB3PostUpdateEventListener implements PostUpdateEventListener,
+ CallbackHandlerConsumer, PostCollectionRecreateEventListener {
EntityCallbackHandler callbackHandler;
public void setCallbackHandler(EntityCallbackHandler callbackHandler) {
@@ -32,10 +38,22 @@
public void onPostUpdate(PostUpdateEvent event) {
Object entity = event.getEntity();
- EntityEntry entry = (EntityEntry)
event.getSession().getPersistenceContext().getEntityEntries().get( entity );
- //mimic the preUpdate filter
- if ( Status.DELETED != entry.getStatus() ) {
- callbackHandler.postUpdate( entity );
+ EventSource session = event.getSession();
+ postUpdate(entity, session);
+ }
+
+ private void postUpdate(Object entity, EventSource session) {
+ EntityEntry entry = (EntityEntry) session.getPersistenceContext()
+ .getEntityEntries().get(entity);
+ // mimic the preUpdate filter
+ if (Status.DELETED != entry.getStatus()) {
+ callbackHandler.postUpdate(entity);
}
}
+
+ public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
+ Object entity = event.getCollection().getOwner();
+ EventSource session = event.getSession();
+ postUpdate(entity, session);
+ }
}
Property changes on:
entitymanager/trunk/src/java/org/hibernate/ejb/event/EJB3PostUpdateEventListener.java
___________________________________________________________________
Name: svn:keywords
- Author Date Id Revision
+ Id
Modified:
entitymanager/trunk/src/test/org/hibernate/ejb/test/callbacks/CallbacksTest.java
===================================================================
---
entitymanager/trunk/src/test/org/hibernate/ejb/test/callbacks/CallbacksTest.java 2008-06-29
13:47:11 UTC (rev 14824)
+++
entitymanager/trunk/src/test/org/hibernate/ejb/test/callbacks/CallbacksTest.java 2008-06-29
13:51:08 UTC (rev 14825)
@@ -1,16 +1,19 @@
//$Id$
package org.hibernate.ejb.test.callbacks;
+import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import org.hibernate.ejb.test.Cat;
+import org.hibernate.ejb.test.Kitten;
import org.hibernate.ejb.test.TestCase;
/**
* @author Emmanuel Bernard
*/
+@SuppressWarnings("unchecked")
public class CallbacksTest extends TestCase {
public void testCallbackMethod() throws Exception {
@@ -161,6 +164,46 @@
em.getTransaction().rollback();
em.close();
}
+
+ /**
+ * Tests callback for collection changes.
+ *
+ * @throws Exception in case the test fails.
+ * @see EJB-288
+ */
+ public void testPostUpdateCollection() throws Exception {
+ EntityManager em = factory.createEntityManager();
+ Cat c = new Cat();
+ em.getTransaction().begin();
+ c.setLength( 23 );
+ c.setAge( 2 );
+ c.setName( "Beetle" );
+ c.setDateOfBirth( new Date() );
+ em.persist( c );
+ em.getTransaction().commit();
+ List ids = Cat.getIdList();
+ Object id = Cat.getIdList().get( ids.size() - 1 );
+ assertNotNull( id );
+
+ // add a kitten to the cat (should trigger a PostCollectionRecreateEvent
+ int postVersion = c.getPostVersion();
+ em.getTransaction().begin();
+ Kitten kitty = new Kitten();
+ List kittens = new ArrayList<Kitten>();
+ kittens.add(kitty);
+ c.setKittens(kittens);
+ em.getTransaction().commit();
+ assertEquals("Post version should have been incremented.", postVersion + 1,
c.getPostVersion());
+
+ // delete the kittens ()
+ postVersion = c.getPostVersion();
+ em.getTransaction().begin();
+ c.getKittens().remove(kitty);
+ em.getTransaction().commit();
+ assertEquals("Post version should have been incremented.", postVersion + 1,
c.getPostVersion());
+
+ em.close();
+ }
public Class[] getAnnotatedClasses() {
return new Class[]{
@@ -169,7 +212,8 @@
Television.class,
RemoteControl.class,
Rythm.class,
- Plant.class
+ Plant.class,
+ Kitten.class
};
}
}
Property changes on:
entitymanager/trunk/src/test/org/hibernate/ejb/test/callbacks/CallbacksTest.java
___________________________________________________________________
Name: svn:keywords
- Author Date Id Revision
+ Id