I am new to Spring and JPA, wasted 5 days and no result with searching internet. I want to save object to SQL SERVER, connection is correct but when I write .flush() I get t
You should introduce your entity manager to your transaction manager, so that when you annotate your function with @Transactional
it loads instances from the pool
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>
<tx:annotation-driven />
HTH!
You have two Spring contexts:
the main one, configured by jpaContext.xml, where beans from the service and repository packages are scanned, and proxied by a transactional interceptor.
the mvc one, configured by the other xml file (you didn't name it) whose role is to describe the MVC part of the application, i.e. define and configure for example the controller beans, the view resolver, etc. This context is a child of the main one.
The problem is that you also scan the service and repository packages in this child context. You thus end up with two instances of each service and repository:
The controller is thus injected with a service coming from the same context as the controller: the not transactional one.
To confirm that, you could add traces in the constructor of the beans and see how many times they are instantiated.
And to avoid the problem, there are two solutions:
To make the story short, try adding @Transactional
at the very beginning of your method. That was an issue in my case.