Spring 3.1 JPA not inserting data while running in tomcat

拟墨画扇 提交于 2019-12-06 06:32:06

Jeremy, thay may be many problems with that and I had similar problem.

In my case I used tomcat and I needed to add spring weaver to tomcat (this problem is described here: http://asrijaffar.blogspot.com/2007/02/spring-jpa-tomcat.html).

In my case I needed to have:

<tx:annotation-driven proxy-target-class="true" />

In DispatcherServlet config.

Additionally in db-context config:

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="jpatest" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
jeremy

Well I actually solved the problem. Here is the solution, there was another stackoverflow issue that solved it:

Spring @Transaction not starting transactions

Basically all that was needed for transactions to fire correctly was for me to add:

<tx:annotation-driven proxy-target-class="true"/>

to my servlet xml. I guess if you just have it in the application config.xml it only works when running under the main class (like in a standalone) if you need to run it in a container you have to declare transaction annotations in the servlet as well.

I also spent several hours trying to figure out where the problem is although my previous application with the same stack is working fine and I wasn't able to understand the difference.

And.... the error was in tag in spring-servlet.xml - it was defined to scan the root package with all the web controller, repository classes, etc.

After changing it to make scanning of the package with web controllers only, the problem was gone..

Just for you (and for me) in case you might meet the same problem, just an additional hint

I also had this same problem, spent couple nights searching - alex solutions saved me - in servlet xml I changed context:component-scan to scan only package with web controller. Example from this page should look like

<context:component-scan base-package="com.naturalbornliar.site.mvc"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!