I am building a maven test project which runs with: Spring MVC 3.1.1 JPA, with Hibernate 4.1.4 as jpa provider MYSQL 5.5.x TOMCAT 7
I'm trying to create a simple end-end basic use case which involves reading from DB and update. Since I like to stick with pure JPA I'm using spring's LocalContainerEntityManagerFactoryBean, injected to my dao using @PersistenceContext annotation.
The problems I had at this point:
- I could not persist to DB (no exception thrown but no db reflection as well).
- I could not delete form DB (no exception thrown but no db reflection as well).
I could although read from the DB.
After going over the documentation I saw that I might missing some JPA support since TOMCAT does not include this out of the box.
I went through this and this and that (and a few more..) and applied the following:
- Added the spring-instrument-tomcat dependency to my pom.xml (just to retrieve the jar).
- I copied that jar to my tomcat's lib directory
Under META-INF directory, I created a context.xml file with the following content:
<Context docBase="SpringTest" path="/SpringTest" reloadable="true" source="org.eclipse.jst.jee.server:SpringTest"> <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader" useSystemClassLoaderAsParent="false" /> </Context>
I added InstrumentationLoadTimeWeaver to my entity-manager definition
And that was supposed to do the trick - but it didn't. I experienced the same behaviour.
Then I encountered this question from stackoverflow. One of the answers suggested to add the transactions definition: <tx:annotation-driven proxy-target-class="true" />
to the DispatcherServlet context (so far I had it only in my root application context). After doing that things started to work out, the transactions got committed to the DB.
At this point I started wondering if all the rest of the stuff are still required, so I rolled back everything I did beside the addition to the DispatcherServlet and it still works perfectly!
SO, my questions: How does it work? Do I really need all those to support JPA in tomcat? And finally, why do I need to copy the declaration to the DispatcherServlet in order to resolve this?
Thanks in advance, Yogi