简体   繁体   中英

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

<?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

<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

<?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. 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 :

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM