Entities Not Persisting - Spring + Hibernate + JPA

坚强是说给别人听的谎言 提交于 2019-11-28 21:57:36

Ok, I figured out the problem. It took me forever to figure it out and had nothing to do with my database config, so I want to help people who have similar issues.

The Spring documentation states the following:

<tx:annotation-driven/> only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put <tx:annotation-driven/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services. See Section 15.2, “The DispatcherServlet” for more information.

What's not posted in my original post is my servlet definition, which has the following lines of configuration code:

myServlet.xml

<context:annotation-config /> 
<context:component-scan base-package="com.myDomain.*" /> 

This brings all annotated beans, including Controllers, Services and Repositories, into the servlet context instead of the application context. And therein lies the problem. When Spring looks for beans annotated with @Transactional (due to the existence of <tx:annotation-driven/> in my config.xml file) it is looking for them in the application context. And, based on my config that was posted in my previous thread, there are no beans being loaded into my application context... they're all in the servlet context. Therefore, when my servlet was calling the beans annotated with @Service & @Transactional it was using beans that were not wrapped by transaction proxies. Thus, no transactions. The trick (rather, the correct way) was to change my config files in the following way:

myServlet.xml

<context:annotation-config /> 
<context:component-scan base-package="com.myDomain.servlets" /> 

config.xml

<context:annotation-config /> 
<context:component-scan base-package="com.myDomain.dao" /> 
<context:component-scan base-package="com.myDomain.services" /> 

This configuration ensures that all Controllers exist in the servlet context and Transactional Services and Repositories exist in the application context, which is where they belong. And finally, after many sleepless nights, my database writes are persisting.

ArunKodakan

We can provide the Controller in servlet-context.xml as follows

    <context:component-scan base-package="com.myDomain" use-default-filters="false" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

This will ensure only Controller exists only in servlet-context. In root-context.xml use the following

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

This will ensure the components other than Controller exists in application context. I searched for this solution a lot, as without this JPA was not updating the database, hope this will help someone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!