Announcement Announcement Module
Collapse
No announcement yet.
Spring managed EntityManager not aware of the ongoing transaction ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring managed EntityManager not aware of the ongoing transaction ?

    Hello,

    I'm trying to implement a DA0 bases on plain JPA as is explained in the official Spring documentation (chapter 12.6.3 - http://static.springframework.org/sp...-jpa-straight).

    I'm using Spring 2.0.2 and Hibernate 3.

    This is my DAO interface:
    Code:
    public interface DataAccessI extends Serializable {
    
    	/**
    	 */
    	public abstract void save(BaseTable table);
    
    	/**
    	 */
    	public abstract void delete(BaseTable table);
    
    	/**
    	 * @return
    	 */
    	public abstract <T extends BaseTable> T find(Class<T> entityClass, int id);
    
    }
    Here is the DAO class:
    Code:
    public class SpringPersistentContextInjectedDataAccess implements DataAccessI {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -799697718286174024L;
    
    	private EntityManager entityManager = null;
    
    	@PersistenceContext( type = PersistenceContextType.EXTENDED)
    	public void setEntityManager(EntityManager em) {
    		this.entityManager = em;
    	}
    
    	public void delete(BaseTable table) {
    		entityManager.remove(entityManager.merge(table));
    	}
    
    	public void save(BaseTable table) {
    
    		entityManager.merge(table);
                    entityManager.flush();
    	}
    
    	.......... 
    
    	}

    here is o part of the spring xml file:

    Code:
    <bean id="PersistenceService"
    		class="com.bo.df.designer.domain.PersistenceService">	
    
    		<property name="persistenceDelegate">
    			<ref bean="SpringPersistentContextInjectedDataAccess" />
    		</property>	
    				
    	</bean>
    
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    
    	<bean id="SpringPersistentContextInjectedDataAccess"
    		class="com.bo.df.designer.dao.jpa.SpringPersistentContextInjectedDataAccess">
    	</bean>
    When in my JUnit tests when the entotyManager.flush() method is called i have a "javax.persistence.TransactionRequiredExceptio n: no transaction is in progress" exception.
    Here is the entire trace:
    Code:
    javax.persistence.TransactionRequiredException: no transaction is in progress
    	at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:293)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    	at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:283)
    	at $Proxy25.flush(Unknown Source)
    	at com.bo.df.designer.dao.jpa.SpringPersistentContextInjectedDataAccess.save(SpringPersistentContextInjectedDataAccess.java:40)
    	at com.bo.df.designer.domain.PersistenceService.save(PersistenceService.java:37)
    	at com.bo.df.designer.domain.BaseTableTest.delete(BaseTableTest.java:89)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    	at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    	at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    	at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    	at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    	at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
    	at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    	at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    	at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    	at org.junit.internal.runners.CompositeRunner.run(CompositeRunner.java:29)
    	at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    	at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    For me the injected EntiyManager should be bound on the ongoing transaction.


    Any idea where is the problem ?
    thank's
Working...
X