0

我似乎无法使用 spring mvc 中注入的 entitymgr 将数据保存在数据库中。我见过多个类似的问题(例如EntityManager cannot use persist to save element to database),但似乎没有一个答案能解决我的问题。这是我的配置:

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

<!-- datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" 
    p:driverClassName="${driver}"
    p:url="${url}" 
    p:username="contact" p:password="contact" />
<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="dataSource" 
    p:packagesToScan="com.rd.web"> <!-- scans for entities (model) -->
    <property name="persistenceProvider">
        <bean class="org.hibernate.ejb.HibernatePersistence" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQL5Dialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"   class="org.springframework.orm.jpa.JpaTransactionManager"
    p:entityManagerFactory-ref="entityManagerFactory" />
<tx:annotation-driven transaction-manager="transactionManager"/>

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

<bean id="contactService" class="com.rd.web.service.ContactServiceImpl"/>
</beans>

我有以下代码(在网络控制器中,但目前已移至服务进行测试):

TEST_CASE1(使用弹簧事务):

@Transactional
public Contact setContact(Contact c){       
        if(c.getId() == null){
            getEMgr().persist(c);   
        }else{
            getEMgr().merge(c);
        }
        return c;
}

==> 没有错误,只是没有插入实体,日志中也没有插入语句。

TEST_CASE2(使用弹簧事务):

@Transactional
public Contact setContact(Contact c){       
        if(c.getId() == null){
            getEMgr().persist(c);   
        }else{
            getEMgr().merge(c);
        }
                    getEMgr().flush();
        return c;
}

==> 我得到的异常:没有正在进行的交易

测试案例3:

    public Contact setContact(Contact c){
    getEMgr().getTransaction().begin();
    try{
        if(c.getId() == null){
            getEMgr().persist(c);               
        }else{
            getEMgr().merge(c);
        }
        getEMgr().flush();
        getEMgr().getTransaction().commit();
        return c;
    }catch(Throwable t){
        getEMgr().getTransaction().setRollbackOnly();
    }
    return null;
}

==> 抛出错误:java.lang.IllegalStateException:不允许在共享 EntityManager 上创建事务 - 改用 Spring 事务或 EJB CMT

它不应该是 Spring AOP 问题,因为该操作是公共的,并且是从另一个组件(服务被注入的组件)调用的。appcontext 还将事务定义为注释驱动程序。我真的不明白为什么我的交易没有开始。

当我使用相同的 applicationcontext.xml 并触发一个加载联系人服务并创建联系人的测试类时,联系人被正确保存。

我还在 web.xml 中添加了以下过滤器,但无济于事:

<filter>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

任何提示将不胜感激。干杯。

添加了一些额外的信息:

@Muel,正在使用持久性上下文注入 entitymgr:

@Transactional

@Service("contactService") 公共类 ContactServiceImpl 实现 IContactService {

// @Autowired // 私有 IEntityMgrProvider eMgrPovider;

@PersistenceContext EntityManager eMgr;

@Transactional
public Contact getContactByID(long id) {
    return getEMgr().find(Contact.class, id);
}

@Transactional
public List<Contact> getAllContacts() {
    TypedQuery<Contact> qry = getEMgr().createNamedQuery("findAll", Contact.class);
    return qry.getResultList();
}

@Transactional
public Contact setContact(Contact c){

        if(c.getId() == null){
            getEMgr().persist(c);
//              getEMgr().flush();
        }else{
            getEMgr().merge(c);
        }
        return c;
}

@Transactional(readOnly=true)
public void deleteContact(long id){
    getEMgr().remove(getEMgr().find(Contact.class, id));

}

private EntityManager getEMgr(){
//      return eMgrPovider.getEMgr();
    return eMgr;
}


public static void main(String[] args) {

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("appctx.xml");
        IContactService contactService = ctx.getBean("contactService", IContactService.class);
        Contact c= new Contact();
        c.setBirthDate(new Date());
        c.setFirstName("P1");
        c.setLastName("P2");

        ContactTelDetail tel = new ContactTelDetail();
        tel.setContact(c);
        tel.setTelNumber("056776650");
        tel.setTelType("landline");

        c = contactService.setContact(c);

        System.out.println(c.getId());

}
}

我意识到这个 getEmgr() 方法不是必需的,但最初 eMgr 来自其他地方(它也被注入,但暂时不要介意)顺便说一句,当我运行 main 方法时,我实际上可以插入联系人......

@user2264997 这是我的 servlet 上下文:

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>   
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

我只添加了最后两个过滤器进行测试。我不认为它应该是必需的(似乎最后一个过滤器仅用于支持延迟加载或其他东西,但无论如何都尝试过......)

@马丁弗雷

我会看看你可能会做些什么。

@mdeinum.wordpress.com

使用@autowired 将服务注入到 web 控制器中。服务实现和 web.xml 见上文。调度程序 servlet 的配置文件(尽管它似乎没有相关信息,但也许这可能是问题;)):

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

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!--    use this dispatcher servlet for root -->
<!--    <default-servlet-handler/> -->


<resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>

<interceptors>
    <beans:bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
    <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
        p:paramName="lang"/>
</interceptors>

<beans:bean
    class="org.springframework.ui.context.support.ResourceBundleThemeSource"
    id="themeSource" />
<beans:bean class="org.springframework.web.servlet.theme.CookieThemeResolver"
    id="themeResolver" p:cookieName="theme" p:defaultThemeName="standard" />


<beans:bean
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
    id="messageSource" p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application"
    p:fallbackToSystemLocale="false" />
<beans:bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
    id="localeResolver" p:cookieName="locale"/>


<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<!--    <resources location="/resources/" mapping="/resources/**" /> -->

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<!--    now using tiles in stead ==> different view resolver -->
<!--    <beans:bean    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> -->
<!--        <beans:property name="prefix" value="/WEB-INF/views/" /> -->
<!--        <beans:property name="suffix" value=".jsp" /> -->
<!--    </beans:bean> -->

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

<!-- Add the following beans -->
<!-- Tiles Configuration -->
<beans:bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
    id="tilesViewResolver">
    <beans:property name="viewClass"
        value="org.springframework.web.servlet.view.tiles2.TilesView" />
</beans:bean>
<beans:bean
    class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
    id="tilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/layouts/layouts.xml</beans:value>
            <!-- Scan views directory for Tiles configurations -->
            <beans:value>/WEB-INF/views/**/views.xml</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

</beans:beans>

我将尝试配置休眠适配器并让您知道它是如何进行的...

干杯

4

2 回答 2

2

您正在 applicationContext.xml 和 servlet-context.xml 中复制组件扫描。

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

当你这样做时,控制器将被注入由 servlet-context.xml 中的组件扫描获取的服务,这些服务没有事务。

在 servlet-context.xml 中为 base-package 显式指定控制器包,在 applicatioContext.xml 中为 base-package 显式指定非控制器包。

或者在组件扫描声明中使用排除/包含过滤器。

应用程序上下文.xml

<context:component-scan ..>
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
 ...

在 servlet-context.xml 中

<context:component-scan ..>
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
 ...
于 2013-08-29T19:17:32.613 回答
0

好的,所以在根应用程序上下文和 dispathcer servlet 应用程序上下文中定义组件扫描确实存在问题。我将控制器移动到一个单独的包中,并且只在应用程序上下文中扫描该包以查找调度程序 servlet,现在一切正常!

于 2013-08-30T11:11:18.843 回答