title: Persist/commit not working in test environment with Spring JPA JUnit
body: <p>I'm trying to setup a basic JPA insert test. But nothing is saved in the DB.
  DB is Postgresql. Hibernate is used as Persistence provider. </p>
  
  <p>Many thanks in advance.</p>
  
  <pre><code>@Entity
  public class Currency {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      protected Integer id;    
  
      @Column
      private String code;
  
      @Column
      private String name;
  ...
  }
  </code></pre>
  
  <p>The CRUD class :</p>
  
  <pre><code>@Repository
  @Scope(BeanDefinition.SCOPE_PROTOTYPE)
  @Transactional(propagation = Propagation.REQUIRED)
  public class CRUDServiceBean implements CRUDService {
  
        @PersistenceContext(type = PersistenceContextType.EXTENDED)
        private EntityManager entityManager;
  
         public EntityManager getEntityManager() {
              return entityManager;
          }
  
          public &lt;T extends BaseEntity&gt; T persistAndCommit(T t) {
              entityManager.persist(t);
              entityManager.refresh(t);
              entityManager.getTransaction().commit();
              return t;
          }
  
          ...
          ...
  }
  </code></pre>
  
  <p>Base class for all tests:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @Configurable(autowire = Autowire.BY_NAME)
  @ContextConfiguration(locations = { "classpath:context-test.xml" })
  public class BaseTest {
  }
  </code></pre>
  
  <p>The test class:</p>
  
  <pre><code>public class CurrencyCreateTest extends BaseTest {
  
      @Autowired
      CRUDService crudService;
  
      @Test
      @Transactional(propagation = Propagation.REQUIRES_NEW)
      public void createCurrency() throws Exception {
          Currency currency = new Currency();
          currency.setCode("EUR");
          currency.setName("Euro");
          currency = crudService.persistAndCommit(currency);
      }
  }
  </code></pre>
  
  <p>context-test.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:task="http://www.springframework.org/schema/task"
         xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
             http://www.springframework.org/schema/tx 
             http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
             http://www.springframework.org/schema/context 
             http://www.springframework.org/schema/context/spring-context-4.0.xsd"&gt;         
  
      &lt;context:component-scan base-package="com.chartinvest"/&gt;
  
      &lt;bean id="contextApplicationContextProvider" class="com.chartinvest.util.ApplicationContextProvider"&gt;&lt;/bean&gt; 
  
  
      &lt;!-- the parent application context definition for the springapp application --&gt;
  
      &lt;!-- dataSource --&gt;
      &lt;bean id="dataSourceFinance" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
        &lt;property name="driverClassName"&gt;&lt;value&gt;org.postgresql.Driver&lt;/value&gt;&lt;/property&gt;
        &lt;property name="url"&gt;&lt;value&gt;jdbc:postgresql://localhost/db_finance_test&lt;/value&gt;&lt;/property&gt;
        &lt;property name="username"&gt;&lt;value&gt;postgres&lt;/value&gt;&lt;/property&gt;
        &lt;property name="password"&gt;&lt;value&gt;xxxxxxxx&lt;/value&gt;&lt;/property&gt;
      &lt;/bean&gt; 
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;       
      &lt;/bean&gt;  
  
      &lt;bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitName" value="mypersistenceunit" /&gt;
          &lt;property name="dataSource" ref="dataSourceFinance" /&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
                  &lt;property name="showSql" value="true" /&gt;
                  &lt;property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
          &lt;property name="jpaPropertyMap"&gt;
              &lt;map&gt;
                  &lt;entry key="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/&gt;
              &lt;/map&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Enable the configuration of transactional behavior based on annotations --&gt;       
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
  &lt;/beans&gt;
  </code></pre>