Hibernate JPA and Spring javax.persistence.TransactionRequiredException: no transaction is in progress

后端 未结 13 2280
说谎
说谎 2020-12-05 17:52

When I call:

entityManager.flush()

I get the exception mentioned in the title.

I am using Hibernate JPA.

相关标签:
13条回答
  • 2020-12-05 18:19

    Same was happening to me using spring 3.0.0 / 3.0.3. Data was persisted in MySQL from JUnit but not from the tomcat server. After so much work I gave up on RESOURCE_LOCAL for JTA.

    This worked for me http://erich.soomsam.net/2007/04/24/spring-jpa-and-jta-with-hibernate-and-jotm/ It uses JTA and depends on JOTM.

    0 讨论(0)
  • 2020-12-05 18:20

    I had the same problem... spent some hours until I found the reason finally. It was just one line of code that caused the exception in my case...

    In my mvc-core-config.xml the following line was the reason:

    <context:component-scan base-package="com.my.package.application" />
    

    After I changed it as follows, everything worked again:

    <context:component-scan base-package="com.my.package.application.controller" />
    

    So I guess the scanning of all my application packages instead of just my @Controller classes lead to the problem like @harshal-waghmare mentioned in his post to another answer.

    0 讨论(0)
  • 2020-12-05 18:22

    My Problem was to do with the way that I setup the <tx:annotation-driven/> Element in my context definition -

    Originally I had load time weaving enabled (not knownley) that read <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/> and by simply removing the 2nd attribute - everything worked (took 2 hours of head banging though). I believe the 2nd element relates to the @Configurable sterotype but can let other (smarter) people explain the difference & why one would work & the other does does not.. Hope this helps...

    working definition= <tx:annotation-driven transaction-manager="transactionManager"/>

    0 讨论(0)
  • 2020-12-05 18:25

    Spring 4.3.1 / Hibernate 4.2.21

    My configuration was 100% Java code with no hibernate or spring xml documents (eg context.xml, persistence.xml etc). The issue was the EntityManagerFactory I was passing to the TransactionManager, see the below configuration in the transactionManager method.

    @Configuration
    @EnableTransactionManagement
    public class HibernateConfiguration2 {
    
    @Bean
    public DataSource dataSource() {
        return ...; // Build a basic datasource
    }
    
    @Bean
    @Autowired
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setPackagesToScan("nz.co.mark");
        entityManagerFactory.setPersistenceProviderClass(org.hibernate.ejb.HibernatePersistence.class);
    
        return entityManagerFactory;
    }
    
    @Bean
    @Autowired
    public EntityManager entityManager(LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean) {
        EntityManager em = localContainerEntityManagerFactoryBean.getNativeEntityManagerFactory().createEntityManager();
        em.setFlushMode(FlushModeType.AUTO);
        return em;
    }
    
    @Bean
    @Autowired
    public JpaTransactionManager transactionManager(LocalContainerEntityManagerFactoryBean emf) throws Exception {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf.getObject());
        // The below line would generate javax.persistence.TransactionRequiredException: no transaction is in progress
        // transactionManager.setEntityManagerFactory(emf.getNativeEntityManagerFactory());
        return transactionManager;
    }
    
    0 讨论(0)
  • 2020-12-05 18:27

    For JBoss 4.0 and Hibernate, I fixed this problem by adding some transaction manager properties to my EntityManagerFactoryBean definition:

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="xaDs" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory
                </prop>
                <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup
                </prop>
            </props>
        </property>
    

    I found the soluton on this message board thread.

    0 讨论(0)
  • 2020-12-05 18:30

    Ensure that you have an active transaction when this statement executes. If you are using JPA use EntityManager.getTransaction().begin(). This is assuming that you are using JPA outside a JTA transaction scope.

    0 讨论(0)
提交回复
热议问题