Author: epbernard
Date: 2008-03-06 11:50:07 -0500 (Thu, 06 Mar 2008)
New Revision: 14393
Modified:
entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/ExceptionInterceptor.java
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/InterceptorTest.java
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/LocalExceptionInterceptor.java
Log:
EJB-340 onLoad() callback from Interceptor and onLoad() from Lifecycle are never invoked
in an EJB3 environment
Modified: entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java
===================================================================
---
entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java 2008-03-05
21:57:56 UTC (rev 14392)
+++
entitymanager/trunk/src/java/org/hibernate/ejb/EventListenerConfigurator.java 2008-03-06
16:50:07 UTC (rev 14393)
@@ -46,6 +46,8 @@
import org.hibernate.event.PreLoadEventListener;
import org.hibernate.event.PreUpdateEventListener;
import org.hibernate.event.SaveOrUpdateEventListener;
+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;
@@ -101,8 +103,13 @@
new JACCPreDeleteEventListener()
}
);
+
+ //Add the default Hibernate Core PreLoadEventListener
+ //TODO shouldn't we read the value from getPreLoadEventListeners and add JACC?
+ //probably a better thing to do as it allows cfg.xml config but this is a big change
and need more thoughts
listenerConfig.setPreLoadEventListeners(
new PreLoadEventListener[] {
+ new DefaultPreLoadEventListener(),
new JACCPreLoadEventListener()
}
);
@@ -114,8 +121,11 @@
listenerConfig.setPostInsertEventListeners(
new PostInsertEventListener[] { new EJB3PostInsertEventListener() }
);
+ //Add the default Hibernate Core PostLoadEventListener
+ //TODO shouldn't we read the value from getPostLoadEventListeners
+ //probably a better thing to do as it allows cfg.xml config but this is a big change
and need more thoughts
listenerConfig.setPostLoadEventListeners(
- new PostLoadEventListener[] { new EJB3PostLoadEventListener() }
+ new PostLoadEventListener[] { new EJB3PostLoadEventListener(), new
DefaultPostLoadEventListener() }
);
listenerConfig.setPostUpdateEventListeners(
new PostUpdateEventListener[] { new EJB3PostUpdateEventListener() }
Modified:
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/ExceptionInterceptor.java
===================================================================
---
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/ExceptionInterceptor.java 2008-03-05
21:57:56 UTC (rev 14392)
+++
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/ExceptionInterceptor.java 2008-03-06
16:50:07 UTC (rev 14393)
@@ -8,92 +8,32 @@
import org.hibernate.EntityMode;
import org.hibernate.Interceptor;
import org.hibernate.Transaction;
+import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
/**
* @author Emmanuel Bernard
*/
-public class ExceptionInterceptor implements Interceptor {
+public class ExceptionInterceptor extends EmptyInterceptor {
public static final String EXCEPTION_MESSAGE = "Interceptor enabled";
+ protected boolean allowSave = false;
- public boolean onLoad(Object entity, Serializable id, Object[] state, String[]
propertyNames, Type[] types)
- throws CallbackException {
- return false; //To change body of implemented methods use File | Settings | File
Templates.
+ public ExceptionInterceptor() {
+ this.allowSave = false;
}
- public boolean onFlushDirty(
- Object entity, Serializable id, Object[] currentState, Object[] previousState,
String[] propertyNames,
- Type[] types
- ) throws CallbackException {
- return false; //To change body of implemented methods use File | Settings | File
Templates.
+ public ExceptionInterceptor(boolean allowSave) {
+ this.allowSave = allowSave;
}
- public boolean onSave(Object entity, Serializable id, Object[] state, String[]
propertyNames, Type[] types)
+ public boolean onLoad(Object entity, Serializable id, Object[] state, String[]
propertyNames, Type[] types)
throws CallbackException {
throw new IllegalStateException( EXCEPTION_MESSAGE );
}
- public void onDelete(Object entity, Serializable id, Object[] state, String[]
propertyNames, Type[] types)
+ public boolean onSave(Object entity, Serializable id, Object[] state, String[]
propertyNames, Type[] types)
throws CallbackException {
- //To change body of implemented methods use File | Settings | File Templates.
+ if (allowSave) return false;
+ throw new IllegalStateException( EXCEPTION_MESSAGE );
}
-
- public void onCollectionRecreate(Object collection, Serializable key) throws
CallbackException {
- //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public void onCollectionRemove(Object collection, Serializable key) throws
CallbackException {
- //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public void onCollectionUpdate(Object collection, Serializable key) throws
CallbackException {
- //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public void preFlush(Iterator entities) throws CallbackException {
- //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public void postFlush(Iterator entities) throws CallbackException {
- //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public Boolean isTransient(Object entity) {
- return null; //To change body of implemented methods use File | Settings | File
Templates.
- }
-
- public int[] findDirty(
- Object entity, Serializable id, Object[] currentState, Object[] previousState,
String[] propertyNames,
- Type[] types
- ) {
- return new int[0]; //To change body of implemented methods use File | Settings | File
Templates.
- }
-
- public Object instantiate(String entityName, EntityMode entityMode, Serializable id)
throws CallbackException {
- return null; //To change body of implemented methods use File | Settings | File
Templates.
- }
-
- public String getEntityName(Object object) throws CallbackException {
- return null; //To change body of implemented methods use File | Settings | File
Templates.
- }
-
- public Object getEntity(String entityName, Serializable id) throws CallbackException {
- return null; //To change body of implemented methods use File | Settings | File
Templates.
- }
-
- public void afterTransactionBegin(Transaction tx) {
- //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public void beforeTransactionCompletion(Transaction tx) {
- //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public void afterTransactionCompletion(Transaction tx) {
- //To change body of implemented methods use File | Settings | File Templates.
- }
-
- public String onPrepareStatement(String sql) {
- return null; //To change body of implemented methods use File | Settings | File
Templates.
- }
}
Modified:
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/InterceptorTest.java
===================================================================
---
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/InterceptorTest.java 2008-03-05
21:57:56 UTC (rev 14392)
+++
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/InterceptorTest.java 2008-03-06
16:50:07 UTC (rev 14393)
@@ -101,6 +101,30 @@
}
}
+ public void testOnLoadCallInInterceptor() {
+ configuration.setInterceptor( new ExceptionInterceptor(true) );
+ EntityManagerFactory emf = configuration.createEntityManagerFactory();
+ EntityManager em = emf.createEntityManager();
+ Item i = new Item();
+ i.setName( "Laptop" );
+ em.getTransaction().begin();
+ em.persist( i );
+ em.flush();
+ em.clear();
+ try {
+ em.find(Item.class, i.getName() );
+ fail( "No interceptor" );
+ }
+ catch (IllegalStateException e) {
+ assertEquals( ExceptionInterceptor.EXCEPTION_MESSAGE, e.getMessage() );
+ }
+ finally {
+ if ( em.getTransaction() != null && em.getTransaction().isActive() )
em.getTransaction().rollback();
+ em.close();
+ emf.close();
+ }
+ }
+
public Class[] getAnnotatedClasses() {
return new Class[]{
Item.class,
Modified:
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/LocalExceptionInterceptor.java
===================================================================
---
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/LocalExceptionInterceptor.java 2008-03-05
21:57:56 UTC (rev 14392)
+++
entitymanager/trunk/src/test/org/hibernate/ejb/test/ejb3configuration/LocalExceptionInterceptor.java 2008-03-06
16:50:07 UTC (rev 14393)
@@ -14,6 +14,7 @@
public boolean onSave(Object entity, Serializable id, Object[] state, String[]
propertyNames, Type[] types)
throws CallbackException {
+ if (allowSave) return false;
throw new IllegalStateException( LOCAL_EXCEPTION_MESSAGE );
}
}