title: Spring3 JPA (Hibernate4) not persisting
body: <p>I have an application using Spring 3.2.3 and Hibernate 4.2.1.Final.
   I made some configurations and the app works pretty well on the test environment, using HSQLDB, etc.</p>
  
  <p>But when the app is deployed it almost works fine. The entity is created but never persisted.
   I can see the JPA logs:</p>
  
  <p>Hibernate: 
      select
          nextval ('TASK_SEQ')</p>
  
  <p>But an insert never appears =(</p>
  
  <p>Using the configuration:</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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"&gt;
  
      &lt;context:component-scan base-package="br.com.company" /&gt;
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitName" value="spring_pu" /&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;context:annotation-config /&gt;
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>persistence.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
      version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"&gt;
      &lt;persistence-unit name="spring_pu" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;class&gt;br.com.company.core.entities.Task&lt;/class&gt;
          &lt;properties&gt;
                  &lt;property name="hibernate.connection.autocommit" value="true" /&gt;
          &lt;property name="hibernate.show_sql" value="true" /&gt;
          &lt;property name="hibernate.format_sql" value="true" /&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" /&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
  
              &lt;property name="hibernate.connection.driver_class" value="org.postgresql.Driver" /&gt;
              &lt;property name="hibernate.connection.url" value="jdbc:postgresql://127.0.0.1:5432/companyDB" /&gt;
              &lt;property name="hibernate.connection.user" value="postgres" /&gt;
              &lt;property name="hibernate.connection.password" value="*****" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt; 
  </code></pre>
  
  <p>The entity:</p>
  
  <pre><code>@Entity
  @Table(name = "TASK")
  public class Task implements Serializable {
  
      private static final long serialVersionUID = -6262731134419520342L;
  
      @Id
      @Column(name = "ID")
      @GeneratedValue(generator = "TASK_SEQ", strategy = GenerationType.SEQUENCE)
      @SequenceGenerator(sequenceName = "TASK_SEQ", name = "TASK_SEQ")
      private long id;
  
      @Column(name = "DESCRIPTION")
      private String description;
  
      @Column(name = "FINISHED")
      private boolean fininshed;
  
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name = "FINISH_DATE")
      private Date finishDate;
  
  //getters and setter below
  }
  </code></pre>
  
  <p>And finally the service:</p>
  
  <pre><code>@Service
  public class TaskService {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      @Transactional(propagation = Propagation.REQUIRED)
      public void createTask(Task task) {
      &lt;b&gt;//invoked method&lt;/b&gt;
          entityManager.persist(task);
      }
  </code></pre>
  
  <p>As I said, there are no exceptions being thrown but the entity is not persisted like it is at the tests.
  Thank you!</p>
  
  <p>Edit: I also tried to remove the persistence.xml content into a spring datasource and the problem is still the same:</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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"&gt;
  
      &lt;bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"&gt;
          &lt;property name="driverClassName" value="org.postgresql.Driver" /&gt;
          &lt;property name="url" value="jdbc:postgresql://127.0.0.1:5432/KCILDS" /&gt;
          &lt;property name="username" value="postgres" /&gt;
          &lt;property name="password" value="*****" /&gt;
      &lt;/bean&gt;
  
      &lt;context:component-scan base-package="br.com.company" /&gt;
  
      &lt;bean id="myEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan" value="br.com.company.core.entities" /&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
          &lt;/property&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="myEntityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;context:annotation-config /&gt;
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>SOLUTION:</strong></p>
  
  <p>I  give up the xml configuration. Nothing seems to work with it anymore.
   Reading more about the tendencies and a lot of configuration I ended up trying successfully a java config and with a few more adjustments will fit perfectly. 
   See below:</p>
  
  <pre><code>    @Configuration
      @EnableTransactionManagement
      @ComponentScan("br.com.company")
      public class PersistenceJPAConfig {
  
          @Bean
          public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
              LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
              factoryBean.setDataSource(this.directDataSource());
              factoryBean.setPackagesToScan(new String[] { "br.com.company" });
  
              HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
              vendorAdapter.setShowSql(true);
  
              factoryBean.setJpaVendorAdapter(vendorAdapter);
              factoryBean.setJpaProperties(this.additionlProperties());
  
              return factoryBean;
          }
  
          private Properties additionlProperties() {
              Properties properties = new Properties();
              properties.put("database", "POSTGRESQL");
              properties.put("databasePlatform", "org.hibernate.dialect.PostgreSQLDialect");
              properties.put(Environment.SHOW_SQL, true);
              properties.put(Environment.FORMAT_SQL, true);
              return properties;
          }
  
  // now reasearch how to make it an environment configuration
      //  @Bean
      //  public DataSource dataSource() {
      //      JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup();
      //      jndiDataSourceLookup.setResourceRef(true);
      //      return jndiDataSourceLookup.getDataSource("jdbc/mybank");
      //  }
  
          @Bean
          public DataSource directDataSource() {
              DriverManagerDataSource dataSource = new DriverManagerDataSource();
              dataSource.setDriverClassName("org.postgresql.Driver");
              dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/MyBank");
              dataSource.setUsername("postgres");
              dataSource.setPassword("*******");
              return dataSource;
          }
  
          @Bean //still trying to make a JTA Transaction
          public PlatformTransactionManager transactionManager() {
              JpaTransactionManager transactionManager = new JpaTransactionManager();
              transactionManager.setEntityManagerFactory(this.entityManagerFactoryBean().getObject());
              return transactionManager;
          }
  
          @Bean
          public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
              return new PersistenceExceptionTranslationPostProcessor();
          }
      }
  </code></pre>