title: entitymanager persist does not save to database
body: <p>I'm currently facing a problem when trying to save to my database using the persist method from an entitymanager. When executing it, it does not produce an exception but it doesn't save the object to my database. Reading objects that were inserted manually does work. </p>
  
  <p><strong>GenericDAOImpl</strong></p>
  
  <pre><code>package be.greg.PaymentDatabase.DAO;
  
  import java.lang.reflect.ParameterizedType;
  import java.lang.reflect.Type;
  import java.util.List;
  import java.util.Map;
  
  import javax.persistence.Entity;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.Query;
  import javax.transaction.Transactional;
  
  public abstract class GenericDaoImpl&lt;T&gt; implements GenericDao&lt;T&gt; {
  
      @PersistenceContext
      protected EntityManager em;
  
      private Class&lt;T&gt; type;
  
      String entity;
  
      @SuppressWarnings({ "unchecked", "rawtypes" })
      public GenericDaoImpl() {
          Type t = getClass().getGenericSuperclass();
          ParameterizedType pt = (ParameterizedType) t;
          type = (Class) pt.getActualTypeArguments()[0];
      }
  
      @Override
      public long countAll(final Map&lt;String, Object&gt; params) {
  
          final StringBuffer queryString = new StringBuffer(
                  "SELECT count(o) from ");
  
          queryString.append(type.getSimpleName()).append(" o ");
          // queryString.append(this.getQueryClauses(params, null));
  
          final Query query = this.em.createQuery(queryString.toString());
  
          return (Long) query.getSingleResult();
  
      }
  
      @Override
      @Transactional
      public T create(final T t) {
  
          this.em.persist(t);
  
          return t;
      }
  
      @Override
      public void delete(final Object id) {
          this.em.remove(this.em.getReference(type, id));
      }
  
      @Override
      public T find(final Object id) {
          return (T) this.em.find(type, id);
      }
  
      @Override
      public T update(final T t) {
          return this.em.merge(t);
      }
  
      @SuppressWarnings("unchecked")
      @Override
      @Transactional
      public List&lt;T&gt; findAll() {
          Query query = em.createQuery("select x from " + getEntityName() + " x");
  
          return (List&lt;T&gt;) query.getResultList();
      }
  
      public String getEntityName() {
  
          if (entity == null) {
              Entity entityAnn = (Entity) type.getAnnotation(Entity.class);
  
              if (entityAnn != null &amp;&amp; !entityAnn.name().equals("")) {
                  entity = entityAnn.name();
              } else {
                  entity = type.getSimpleName();
              }
          }
  
          return entity;
      }
  
  }
  </code></pre>
  
  <p><strong>AuthorizationV2DAOImpl</strong></p>
  
  <pre><code>    package be.greg.PaymentDatabase.DAO;
  
  import org.springframework.stereotype.Repository;
  
  import be.greg.PaymentDatabase.model.Authorization;
  
  @Repository
  public class AuthorizationV2DAOImpl extends GenericDaoImpl&lt;Authorization&gt;
          implements AuthorizationV2DAO {
  
  }
  </code></pre>
  
  <p><strong>AuthorizationService</strong></p>
  
  <pre><code>package be.greg.PaymentDatabase.service;
  
  import java.util.List;
  
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import be.greg.PaymentDatabase.DAO.AuthorizationV2DAOImpl;
  import be.greg.PaymentDatabase.model.Authorization;
  
  @Service
  public class AuthorizationService {
      @Autowired
      private AuthorizationV2DAOImpl authorizationDAO;
  
  
  
      public Authorization getAuthorization(int id){
  
  
          return authorizationDAO.find(id);
  
      }
  
      public List&lt;Authorization&gt; getAllAuthorizations(){
  
          return authorizationDAO.findAll();
      }
  
  
      public void createAuthorization(Authorization authorization)
      {
  
          authorizationDAO.create(authorization);
      }
  
  
  
  }
  </code></pre>
  
  <p><strong>Authorization</strong></p>
  
  <pre><code>package be.greg.PaymentDatabase.model;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "authorization")
  public class Authorization {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
      @Column(length = 32, nullable = false)
      private String clientId;
      @Column(length = 32, nullable = false)
      private String acquiringInstitutionId;
      @Column(length = 32, nullable = false)
      private String cardAcceptorTerminalId;
      @Column(length = 32, nullable = false)
      private String merchantTransactionTimestamp;
      @Column(length = 32, nullable = false)
      private String industry;
      @Column(length = 32, nullable = false)
      private String accountNumber;
      @Column(nullable = false)
      private boolean maskedAccount;
      @Column(length = 11, nullable = false)
      private int expiryMonth;
      @Column(length = 11, nullable = false)
      private int expiryYear;
      @Column(length = 32, nullable = false)
      private String securityCode;
      @Column(length = 32, nullable = false)
      private String line1;
      @Column(length = 32, nullable = true)
      private String line2;
      @Column(length = 32, nullable = true)
      private String city;
      @Column(length = 32, nullable = true)
      private String countrySubdivision;
      @Column(length = 32, nullable = false)
      private String postalCode;
      @Column(length = 32, nullable = false)
      private String country;
      @Column(length = 32, nullable = false)
      private String clientReference;
      @Column(length = 32, nullable = false)
      private String currency;
      @Column(length = 11, nullable = false)
      private int value;
      @Column(length = 32, nullable = false)
      private String ecommerceIndicator;
      @Column(length = 32, nullable = false)
      private String transactionId;
      @Column(length = 32, nullable = false)
      private String token;
  
          Constructor, getters and setters ...    
  </code></pre>
  
  <p><strong>spring-context.xml</strong></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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"&gt;
      &lt;context:component-scan base-package="be.greg.PaymentDatabase" /&gt;
  
  
      &lt;mvc:annotation-driven /&gt;
  
      &lt;bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jdbcPropertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="classpath:project.properties" /&gt;
  
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
          p:username="${jdbc.username}" /&gt;
  
      &lt;bean id="persistenceUnitManager"
          class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt;
          &lt;property name="persistenceXmlLocations"&gt;
              &lt;list&gt;
                  &lt;value&gt;classpath*:META-INF/persistence.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="defaultDataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitManager" ref="persistenceUnitManager" /&gt;
          &lt;property name="persistenceUnitName" value="entityManager" /&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;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>persistance.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0"
      xmlns="http://java.sun.com/xml/ns/persistence"&gt;
      &lt;persistence-unit name="entityManager"
          transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
          &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
          &lt;validation-mode&gt;NONE&lt;/validation-mode&gt;
          &lt;class&gt;be.greg.PaymentDatabase.model.Authorization&lt;/class&gt;
          &lt;class&gt;be.greg.PaymentDatabase.model.AuthorizationResponse&lt;/class&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
              &lt;property name="hibernate.show_sql" value="false" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>hibernate.cfg.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
  
  
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
          &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/paymentdatabase?zeroDateTimeBehavior=convertToNull&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
  
          &lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;
          &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
          &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;
  
  
          &lt;mapping class="be.greg.PaymentDatabase.model.Authorization" /&gt;
          &lt;mapping class="be.greg.PaymentDatabase.model.AuthorizationResponse" /&gt;
  
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>runnable main class</strong></p>
  
  <pre><code>package be.greg.PaymentDatabase.Tests;
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  import org.springframework.stereotype.Component;
  
  import be.greg.PaymentDatabase.model.Authorization;
  import be.greg.PaymentDatabase.service.AuthorizationService;
  
  @Component
  public class HibernateDatabaseTest {
  
      public static void main(String[] args) {
  
          @SuppressWarnings("resource")
          ApplicationContext context = new ClassPathXmlApplicationContext(
                  "/spring-context.xml");
          HibernateDatabaseTest p = context.getBean(HibernateDatabaseTest.class);
          Authorization authorization = new Authorization("000091095650", "1340",
                  "001", "2012-01-06T09:30:47Z", "MOTO", "4417122900000002",
                  false, 12, 12, "382", "100", null, null, null, "33606", "USA",
                  "12345678901234567", "USD", 1540, "5",
                  "Q0JLSkIyODlWMVBaTDRURFhYV0Y=", "Q0JLSkIyODlWMVBaTDRURFhYV0Y=");
          p.start(authorization);
  
      }
  
      @Autowired
      private AuthorizationService authorizationService;
  
      private void start(Authorization authorization) {
  
          authorizationService.createAuthorization(authorization);
  
          List&lt;Authorization&gt; list = authorizationService.getAllAuthorizations();
          for (Authorization authorizations : list) {
              System.out.println(authorizations.getClientId());
          }
      }
  
  }
  </code></pre>
  
  <p>When I add em.flush in the GenericDaoImpl class right after the persist, it gives following exception </p>
  
  <pre><code>Exception in thread "main" javax.persistence.TransactionRequiredException: no transaction is in progress
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
      at com.sun.proxy.$Proxy24.flush(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
      at com.sun.proxy.$Proxy20.flush(Unknown Source)
      at be.greg.PaymentDatabase.DAO.GenericDaoImpl.create(GenericDaoImpl.java:50)
      at be.greg.PaymentDatabase.service.AuthorizationService.createAuthorization(AuthorizationService.java:35)
      at be.greg.PaymentDatabase.Tests.HibernateDatabaseTest.start(HibernateDatabaseTest.java:36)
      at be.greg.PaymentDatabase.Tests.HibernateDatabaseTest.main(HibernateDatabaseTest.java:27)
  </code></pre>
  
  <p>So I assume it has to do something with the transaction or perhaps the fact that one hasn't been made. But I have not found the cause for this problem yet</p>
  
  <p>Thanks in advance!</p>
  
  <p><em>Edit</em></p>
  
  <p>These are the dependencies for Spring and Hibernate that I use</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
          &lt;version&gt;3.2.8.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
          &lt;version&gt;3.2.8.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;version&gt;3.2.8.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
  
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
          &lt;version&gt;3.2.8.RELEASE&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;4.3.4.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;4.3.4.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
  
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;version&gt;5.1.29&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>