(I am asking the same question in StackOverflow (http://stackoverflow.com/questions/12889462/auto-flush-not-working-for-spring-3-1-localentitymanagerfactorybean-hibernate]StackOverflow ) and Spring Forum ( http://forum.springsource.org/showthread.php?131051-Auto-Flush-not-working-for-Spring-3-1-LocalEntityManagerFactoryBean-Hibernate-4-JTA )
However I am really confused about the possible cause of the problem, so I am seeking help in Hibernate too)
I am using Spring 3.1.2.RELEASE, Spring Data JPA, JPA with Hibernate 4.1.6.Final, JTA with Websphere 8. I am unable to get auto-flush working upon commit.
I have did a lot of search on the web and pages like
http://forum.springsource.org/showthread.php?126363-Session-not-flushed-with-Hibernate-4-1-3-JTA-and-Spring-transaction-management-integ]
and https://jira.springsource.org/browse/SPR-9480
and https://jira.springsource.org/browse/SPR-9404 .
They mostly suggested for using CMTTransactionFactory. However I am still unable to make it work (I wonder if it is because I am creating a EM using a LocalContainerEntityManagerFactoryBean instead of creating Hibernate session by LocalSessionFactoryBean).
I am making use of Spring Data JPA for the persistence layer
My App Service looks something like:
Code:
@Transactional(propagation=Propagation.REQUIRES_NEW)
public class FooServiceImpl implements FooService {
@Inject
private FooRepository fooRepo;
public void doSomething() {
Foo foo = fooRepo.findBySomething(...);
foo.setBar(...);
//fooRepo.flush(); // un-comment to do manual-flushing
}
}
Here is the snippet in my app ctx
Code:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/fooDs" />
<bean id="transactionManager"
class="org.springframework.transaction.jta.WebSphereUowTransactionManager" />
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
</bean>
</property>
<property name="persistenceUnitName" value="foo" />
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.foo" />
<!--
<property name="persistenceXmlLocation" value="classpath:/META-INF/test-persistence.xml" />
-->
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
<entry key="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/>
<entry key="hibernate.transaction.flush_before_completion" value = "true" />
</map>
</property>
</bean>
I am making use of Spring Data JPA and after the entity is retrieved, I updated the content. If I am not flushing the repository manually (which means flushing session/entityManager), it simply discard my changes.
Can anyone give me some hints on what I have missed in the setting? Is there anything special to notice for using LocalContainerEntityManagerFactoryBean?
I tried to change the jpaProperty entry of manager_lookup_class to
`hibernate.transaction.jta.platform => org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform` but it does not help.
I also tried to use `org.hibernate.transaction.JTATransactionFactory` as `transaction factory class`, or even remove the `transaction factory class`. Such change seems makes Hibernate unable to pickup the transaction. When I am manually flushing the repository, it simply give me a `javax.persistence.TransactionRequiredException` with message `no transaction is in progress`
Is there anyone using the above combination and have a working config that I can follow?
--------
Edit: Just a little update: I tried to inject EntityManager to my service by @PersistenceContext, and use JPA directly instead of going through Spring Data JPA. Same things happens: Unless I flush the entity manager explicitly, Hibernate is not picking up my updated entity to issue update statement. At least we know the problem has nothing to do with Spring Data