spring mvc - 多个上下文 - javax.persistence.TransactionRequiredException:没有事务性EntityManager可用

时间:2015-07-29 16:11:47

标签: java spring hibernate spring-mvc jpa

我正在使用带有hibernate adn jpa的spring mvc,以及多个spring上下文(“标准”根上下文加上servlet上下文,这可能是我遇到的其他几个帖子出现问题的原因,但是几种组合后我没有发现问题...... 但是,在我的具体情况下,我不知道哪些声明应该从一个上下文转移到另一个......

当我尝试持久化实体时,例外是:

javax.persistence.TransactionRequiredException: No transactional EntityManager available

应该完成工作的服务方法是

package org.wl.orm.services;

@Service
public class ContactManager {

  @Autowired
  private ContactDaoImpl dao;
  ...
  @Transactional
  public void persist(Contact contact){
      dao.persist(contact);
  }
  ...
}

root-context.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd"
>

    <!-- Datasource -->  
   <bean id="contactDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
      <property name="url" value="jdbc:hsqldb:res:/db/crudapp;shutdown=true" />
      <property name="username" value="sa" />
      <property name="password" value="" />
   </bean>

    <!--  entity manager -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="contactDS" />
        <property name="packagesToScan" value="org.wl.orm" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>                
            </props>
        </property>
    </bean>

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


    <!-- exceptions -->
   <bean id="persistenceExceptionTranslationPostProcessor"
      class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <context:component-scan base-package="org.wl.orm" />
    <tx:annotation-driven transaction-manager="transactionManager" />


</beans>

servlet上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd"
>

    <!-- declare static resources path -->
    <mvc:resources location="/static/" mapping="/static/**"></mvc:resources>

    <!--  declare bundles path -->
    <bean 
        id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource"
    >
        <property name="basename">
            <value>messages</value>
        </property>
    </bean>

    <!--  configure the view resolver -->
    <bean id="viewResolver"
            class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--  SPECIFIC -->

    <!--  Add the view name in the model -->
    <mvc:interceptors>
        <bean class="org.wl.interceptors.ViewNameInModelInterceptor" />
    </mvc:interceptors>

    <!-- scan and use annotations -->
    <context:component-scan base-package="org.wl.controller" />

    <mvc:annotation-driven  />
</beans>

总而言之,我只在我的servlet上下文中扫描spring mvc控制器,以及在我的根上下文中与hibenrate / services相关的所有内容。

你知道我做得不好吗?

提前致谢。

0 个答案:

没有答案