Author: epbernard Date: 2006-07-07 06:55:11 -0400 (Fri, 07 Jul 2006) New Revision: 10096 Added: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/RefreshTest.java Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/event/CallbackResolver.java trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java Log: EJB-205 refresh chech contains before delegating to refresh Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java =================================================================== --- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java 2006-07-06 16:07:04 UTC (rev 10095) +++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java 2006-07-07 10:55:11 UTC (rev 10096) @@ -243,6 +243,9 @@ checkTransactionNeeded(); //adjustFlushMode(); try { + if ( ! getSession().contains( entity ) ) { + throw new IllegalArgumentException( "Entity not managed" ); + } getSession().refresh( entity ); } catch (MappingException e) { Modified: trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/event/CallbackResolver.java =================================================================== --- trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/event/CallbackResolver.java 2006-07-06 16:07:04 UTC (rev 10095) +++ trunk/HibernateExt/ejb/src/java/org/hibernate/ejb/event/CallbackResolver.java 2006-07-07 10:55:11 UTC (rev 10096) @@ -55,40 +55,39 @@ boolean stopListeners = false; boolean stopDefaultListeners = false; do { - //FIXME exclude overriden callback methods Callback callback = null; List<XMethod> methods = currentClazz.getDeclaredMethods(); final int size = methods.size(); for ( int i = 0; i < size ; i++ ) { final XMethod xMethod = methods.get( i ); if ( xMethod.isAnnotationPresent( annotation ) ) { - if ( callback == null ) { - Method method = reflectionManager.toMethod( xMethod ); - callback = new BeanCallback( method ); - Class returnType = method.getReturnType(); - Class[] args = method.getParameterTypes(); - if ( returnType != Void.TYPE || args.length != 0 ) { - throw new RuntimeException( - "Callback methods annotated on the bean class must return void and take no arguments: " + annotation - .getName() + " - " + xMethod - ); - } - if ( ! method.isAccessible() ) { - method.setAccessible( true ); - } - final String methodName = method.getName(); - if ( ! callbacksMethodNames.contains( methodName) ) { - //overriden method, remove the superclass overriden method + Method method = reflectionManager.toMethod( xMethod ); + final String methodName = method.getName(); + if ( ! callbacksMethodNames.contains( methodName ) ) { + //overriden method, remove the superclass overriden method + if ( callback == null ) { + callback = new BeanCallback( method ); + Class returnType = method.getReturnType(); + Class[] args = method.getParameterTypes(); + if ( returnType != Void.TYPE || args.length != 0 ) { + throw new RuntimeException( + "Callback methods annotated on the bean class must return void and take no arguments: " + annotation + .getName() + " - " + xMethod + ); + } + if ( ! method.isAccessible() ) { + method.setAccessible( true ); + } callbacks.add( 0, callback ); //superclass first callbacksMethodNames.add( 0, methodName ); } + else { + throw new PersistenceException( + "You can only annotate one callback method with " + + annotation.getName() + " in bean class: " + beanClass.getName() + ); + } } - else { - throw new PersistenceException( - "You can only annotate one callback method with " - + annotation.getName() + " in bean class: " + beanClass.getName() - ); - } } } if ( !stopListeners ) { @@ -123,50 +122,59 @@ Callback callback = null; if ( listener != null ) { XClass xListener = reflectionManager.toXClass( listener ); - List<XMethod> methods = xListener.getDeclaredMethods(); - final int size = methods.size(); - for ( int i = 0; i < size ; i++ ) { - final XMethod xMethod = methods.get(i); - final Method method = reflectionManager.toMethod( xMethod ); - if ( xMethod.isAnnotationPresent( annotation ) ) { - if ( callback == null ) { - try { - callback = new ListenerCallback( method, listener.newInstance() ); + callbacksMethodNames = new ArrayList<String>(); + do { + List<XMethod> methods = xListener.getDeclaredMethods(); + final int size = methods.size(); + for ( int i = 0; i < size ; i++ ) { + final XMethod xMethod = methods.get(i); + if ( xMethod.isAnnotationPresent( annotation ) ) { + final Method method = reflectionManager.toMethod( xMethod ); + final String methodName = method.getName(); + if ( ! callbacksMethodNames.contains( methodName ) ) { + //overriden method, remove the superclass overriden method + if ( callback == null ) { + try { + callback = new ListenerCallback( method, listener.newInstance() ); + } + catch (IllegalAccessException e) { + throw new PersistenceException( + "Unable to create instance of " + listener.getName() + + " as a listener of beanClass", e + ); + } + catch (InstantiationException e) { + throw new PersistenceException( + "Unable to create instance of " + listener.getName() + + " as a listener of beanClass", e + ); + } + Class returnType = method.getReturnType(); + Class[] args = method.getParameterTypes(); + if ( returnType != Void.TYPE || args.length != 1 ) { + throw new PersistenceException( + "Callback methods annotated in a listener bean class must return void and take one argument: " + annotation + .getName() + " - " + method + ); + } + if ( ! method.isAccessible() ) { + method.setAccessible( true ); + } + callbacks.add( 0, callback ); // listeners first + } + else { + throw new PersistenceException( + "You can only annotate one callback method with " + + annotation.getName() + " in bean class: " + beanClass.getName() + " and callback listener: " + + listener.getName() + ); + } } - catch (IllegalAccessException e) { - throw new PersistenceException( - "Unable to create instance of " + listener.getName() - + " as a listener of beanClass", e - ); - } - catch (InstantiationException e) { - throw new PersistenceException( - "Unable to create instance of " + listener.getName() - + " as a listener of beanClass", e - ); - } - Class returnType = method.getReturnType(); - Class[] args = method.getParameterTypes(); - if ( returnType != Void.TYPE || args.length != 1 ) { - throw new PersistenceException( - "Callback methods annotated in a listener bean class must return void and take one argument: " + annotation - .getName() + " - " + method - ); - } - if ( ! method.isAccessible() ) { - method.setAccessible( true ); - } - callbacks.add( 0, callback ); // listeners first } - else { - throw new PersistenceException( - "You can only annotate one callback method with " - + annotation.getName() + " in bean class: " + beanClass.getName() + " and callback listener: " - + listener.getName() - ); - } } + xListener = null; //xListener.getSuperclass(); } + while (xListener != null); } } return callbacks.toArray( new Callback[ callbacks.size() ] ); Modified: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java =================================================================== --- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java 2006-07-06 16:07:04 UTC (rev 10095) +++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/QueryTest.java 2006-07-07 10:55:11 UTC (rev 10096) @@ -23,7 +23,7 @@ em.persist( item ); item = new Item( "Computer", "Apple II" ); em.persist( item ); - Query q = em.createQuery( "select i from Item i where i.name like :itemName" ); + Query q = em.createQuery( "select i from " + Item.class.getName() + " i where i.name like :itemName" ); q.setParameter( "itemName", "%" ); q.setMaxResults( 1 ); q.getSingleResult(); Added: trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/RefreshTest.java =================================================================== --- trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/RefreshTest.java 2006-07-06 16:07:04 UTC (rev 10095) +++ trunk/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/RefreshTest.java 2006-07-07 10:55:11 UTC (rev 10096) @@ -0,0 +1,39 @@ +//$Id: $ +package org.hibernate.ejb.test.emops; + +import javax.persistence.EntityManager; + +import org.hibernate.ejb.test.TestCase; + +/** + * @author Emmanuel Bernard + */ +public class RefreshTest extends TestCase { + + public void testRefreshNonManaged() throws Exception { + EntityManager em = factory.createEntityManager(); + em.getTransaction().begin(); + Race race = new Race(); + em.persist( race ); + em.flush(); + em.clear(); + + try { + em.refresh( race ); + fail("Refresh should fail on a non managed entity"); + } + catch( IllegalArgumentException e) { + //success + } + + em.getTransaction().rollback(); + em.close(); + } + + public Class[] getAnnotatedClasses() { + return new Class[] { + Race.class, + Competitor.class + }; + } +} |