简体   繁体   English

请求处理失败; 嵌套异常为javax.persistence.TransactionRequiredException:没有可用的事务EntityManager

[英]Request processing failed; nested exception is javax.persistence.TransactionRequiredException: No transactional EntityManager available

An exception occurs while I'm trying to persist an object. 当我尝试保留对象时发生异常。

import org.springframework.transaction.annotation.Transactional;

@Repository("DBRepository")
@Transactional
public class DBRepository implements Repository {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void addElement(Element element) {
        logger.debug("Element {} is going to be added to database", element);
        entityManager.persist(element);
    }
}

persistence.xml persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="p_unit" transaction-type="RESOURCE_LOCAL"/>

</persistence>

web.xml web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/jpa-config.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

jpa-config.xml jpa-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- Using annotation to configure application -->
    <context:annotation-config/>

    <context:component-scan base-package="web.tmh"/>

    <!-- Tells Spring to use persistence context annotation -->
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

    <tx:annotation-driven mode="proxy" transaction-manager="transactionManager"/>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="p_unit"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true"/>
            </bean>
        </property>
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
                <entry key="hibernate.hbm2ddl.auto" value="update"/>
            </map>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <!-- autoReconnect=true param allows our application to reconnect to base if connection was lost -->
        <property name="url" value="jdbc:mysql://localhost:3306/TellMeHow?autoReconnect=true"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

</beans>

So as you can see I used right Transactional annotation, and have <tx:annotation-driven mode="proxy" transaction-manager="transactionManager"/> in my jpa-config.xml. 因此,您可以看到我使用了正确的Transactional批注,并且在jpa-config.xml中具有<tx:annotation-driven mode="proxy" transaction-manager="transactionManager"/> So I don't really understand why it doesn't want do work 所以我真的不明白为什么它不想要工作

Ok, the problem was solved. 好的,问题解决了。 I've made a small mistake in my dispatcher-config.xml : 我在dispatcher-config.xml犯了一个小错误:

So instead of 所以代替

<mvc:annotation-driven/>
<context:component-scan base-package="web.tmh"/>

I had to write 我不得不写

<mvc:annotation-driven/>
<context:component-scan base-package="web.tmh.controller"/>

I thought that component-scan worked recursively, but seems like it didn't... 我以为component-scan递归地工作,但似乎没有...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate Spring JPA javax.persistence.TransactionRequiredException:没有可用的事务性EntityManager - Hibernate Spring JPA javax.persistence.TransactionRequiredException: No transactional EntityManager available javax.persistence.TransactionRequiredException: 没有 EntityManager 与当前线程可用的实际事务 - javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread 没有正在进行的交易; 嵌套异常是 javax.persistence.TransactionRequiredException: no transaction is in progress - No transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress javax.persistence.TransactionRequiredException: - javax.persistence.TransactionRequiredException: 错误javax.persistence.TransactionRequiredException:没有事务正在进行 - Error javax.persistence.TransactionRequiredException: no transaction is in progress 更新/删除查询中的javax.persistence.TransactionRequiredException - javax.persistence.TransactionRequiredException in update/delete query javax.persistence.TransactionRequiredException的可能原因 - Possible causes of javax.persistence.TransactionRequiredException 小面应用程序中的javax.persistence.TransactionRequiredException - javax.persistence.TransactionRequiredException in small facelet application javax.persistence.TransactionRequiredException:当前没有活动的事务 - javax.persistence.TransactionRequiredException: There is no currently active transaction javax.persistence.TransactionRequiredException:PuId =没有活动事务 - javax.persistence.TransactionRequiredException: No active transaction for PuId=
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM