Announcement Announcement Module
Collapse
No announcement yet.
findAll and TransactionManager error Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • findAll and TransactionManager error

    Hi,

    I want to test the following service, but I have an error.
    I have a bean named txManager

    Code:
    @Service
    public class MovieServiceImpl {
        @Autowired
        private MovieRepository repository;
    
        @Transactional( propagation = Propagation.SUPPORTS,readOnly = true )
        public List<Movie> getAllMovies() {
            return (List<Movie>) repository.findAll();
        }
    }
    And:

    Code:
    public interface MovieRepository extends CrudRepository<Movie, Long> {
    }
    My Junit Test:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:applicationContext.xml")
    @TransactionConfiguration(defaultRollback = true )
    public class MovieServiceImplTest {
        @Transactional
        @Test
        public void testFindAllRepo(){
            List<Movie> films = movieService.getAllMovies();
        }
    Code:
        <bean id="myEmf"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="packagesToScan" value="com.xxxxx" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="showSql" value="true" />
                    <property name="generateDdl" value="false" />
                    <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
                </bean>
            </property>
        </bean>
        <bean id="dataSource"
              class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql:xxxxx" />
            <property name="username" value="xxxxx" />
            <property name="password" value="xxxxx" />
        </bean>
    
        <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="myEmf" />
        </bean>
    
        <tx:annotation-driven transaction-manager="txManager" />
    With:
    Code:
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-jpa</artifactId>
                <version>1.3.0.RELEASE</version>
            </dependency>
    And Spring 3.2.2.RELEASE

    I have this error:

    org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'transactionManager' is defined
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:568)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedLocalBeanDefinition(AbstractB eanFactory.java:1102)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:278 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.transaction.interceptor.Transa ctionAspectSupport.determineTransactionManager(Tra nsactionAspectSupport.java:331)
    at org.springframework.transaction.interceptor.Transa ctionAspectSupport.invokeWithinTransaction(Transac tionAspectSupport.java:250)
    at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:94)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
    at org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor.invoke(PersistenceExcepti onTranslationInterceptor.java:155)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
    at org.springframework.data.jpa.repository.support.Lo ckModeRepositoryPostProcessor$LockModePopulatingMe thodIntercceptor.invoke(LockModeRepositoryPostProc essor.java:92)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
    at org.springframework.aop.interceptor.ExposeInvocati onInterceptor.invoke(ExposeInvocationInterceptor.j ava:91)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
    at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy24.findAll(Unknown Source)
    at MovieServiceImpl.getAllMovies(MovieServiceImpl.jav a:32)

    And finally I tried with @Transactional(value = "txManager") , but it doesn't work.

    Any Helps will be appreciate.

    Thanks.

    Regards.

  • #2
    Spring test expects a bean named transactionManager unless you specify otherwise. Specify the correct name on the TransactionConfiguration annotation.

    Comment

    Working...
    X