title: Auto Flush not working for Spring 3.1 LocalEntityManagerFactoryBean + Hibernate 4 + Websphere JTA
body: <p>I have did a lot of search on the web and pages like <a href="http://forum.springsource.org/showthread.php?126363-Session-not-flushed-with-Hibernate-4-1-3-JTA-and-Spring-transaction-management-integ" rel="nofollow">this</a> and <a href="https://jira.springsource.org/browse/SPR-9480" rel="nofollow">this</a> and <a href="https://jira.springsource.org/browse/SPR-9404" rel="nofollow">this</a> 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)</p>
  
  <p>My App Service looks something like:</p>
  
  <pre><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
    }
  }
  </code></pre>
  
  <p>Here is the snippet in my app ctx</p>
  
  <pre><code>&lt;jee:jndi-lookup  id="dataSource" jndi-name="jdbc/fooDs" /&gt;
  
  &lt;bean id="transactionManager"
      class="org.springframework.transaction.jta.WebSphereUowTransactionManager" /&gt;
  
  &lt;bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" /&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="persistenceUnitName" value="foo" /&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="packagesToScan" value="com.foo" /&gt;
      &lt;!--
        &lt;property name="persistenceXmlLocation" value="classpath:/META-INF/test-persistence.xml" /&gt;
       --&gt;
      &lt;property name="jpaPropertyMap"&gt;
          &lt;map&gt;
              &lt;entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" /&gt;
                &lt;entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" /&gt;
                &lt;entry key="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/&gt;
  
              &lt;entry key="hibernate.transaction.flush_before_completion"  value = "true" /&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>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 session/entityManager) manually, it simply discard my changes.</p>
  
  <p>Can anyone give me some hints on what I have missed in the setting?  Is there anything special to notice for using LocalContainerEntityManagerFactoryBean?</p>
  
  <p>I tried to change the jpaProperty entry of manager_lookup_class to 
  <code>hibernate.transaction.jta.platform  =&gt; org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform</code> but it does not help.</p>
  
  <p>I also tried to use <code>org.hibernate.transaction.JTATransactionFactory</code> as <code>transaction factory class</code>, or even remove the <code>transaction factory class</code>.  Such change seems makes Hibernate unable to pickup the transaction.  When I am manually flushing the repository, it simply give me a <code>javax.persistence.TransactionRequiredException</code> with message <code>no transaction is in progress</code></p>
  
  <p>I am using Spring 3.1.2.RELEASE, JPA with Hibernate 4.1.6.Final, Websphere 8</p>
  
  <p>Is there anyone using the above combination and have a working config that I can follow? </p>
  
  <hr>
  
  <p>Edit: I have tried to inject EntityManager to the service to avoid using Spring Data JPA.  The symptom is the same: I have to explicitly call entityManager.flush() to let Hibernate pickup the updated entities.  At least we can take Spring Data out of picture :)</p>