簡體   English   中英

Spring MVC + JPA + Hibernate +交易問題

[英]spring mvc + jpa + hibernate + transaction issue

我似乎無法在Spring MVC中使用注入的Entitymgr將數據持久保存在數據庫中。 我已經看到多個類似的問題(例如EntityManager無法使用persist將元素保存到數據庫 ),但是似乎沒有任何答案可以解決我的問題。 這是我的配置:

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

我有以下代碼(位於Web控制器中,但當前已移至服務進行測試):

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;
}

==>我得到異常:沒有事務在進行中

TEST_CASE3:

    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並僅觸發一個用於加載contactservice並創建聯系人的測試類時,該聯系人將被正確保存。

另外,我在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,使用persistencecontext注入了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方法時,我實際上可以插入Contact ...

@ 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將服務注入到webcontroller中。 服務實現和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>

我將嘗試配置休眠適配器,並讓您知道它的運行方式...

干杯

您將在applicationContext.xml和servlet-context.xml中復制組件掃描。

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

當您執行此操作時,控制器將注入由servlet-context.xml中沒有事務的組件掃描所選擇的服務。

要么在servlet-context.xml中顯式為基本軟件包指定控制器軟件包,要么在applicatioContext.xml中為基本軟件包指定非控制器軟件包。

或在組件掃描聲明中使用排除/包含過濾器。

applicationContext.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" />
 ...

好吧,因此在根應用程序上下文和無視Servlet應用程序上下文中定義組件掃描確實存在問題。 我將控制器移到一個單獨的程序包,僅在應用程序上下文中掃描了該程序包中的調度程序servlet,現在一切正常!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM