title: Unable to persist data into mysql database using spring + spring data jpa, Why?
body: <p>I am developing <strong>Spring + JPA example</strong>. In this example, I was able to find the <code>Product</code> based on <code>ProductCode</code>, but Unable to save Product using <code>Junit</code>. Please guide whats wrong in the code. I also I see that for each <code>junit</code> run the <code>auto_incremented</code> <code>product_id</code> is auto <code>increment</code>, but entity not getting saved. </p>
  
  <p><strong>Note:</strong> I followed many links from the stackoverflow, but nothing get worked for me.</p>
  
  <p><strong>Product.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "product")
  public class Product {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "product_id")
      private int id;
  
      @Column(name = "product_code")
      private String productCode;
  
      @Column(name = "product_name")
      private String productName;
  
      @Column(name = "price")
      private float price;
  
      @Column(name = "vat")
      private float vat;
  
      @Transient
      @Formula("price * 2")
      private float totalPrice;
  
      @Column(name = "manufactured_date")
      @Temporal(TemporalType.TIMESTAMP)
      private Date manufacturedDate;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getProductCode() {
          return productCode;
      }
  
      public void setProductCode(String productCode) {
          this.productCode = productCode;
      }
  
      public String getProductName() {
          return productName;
      }
  
      public void setProductName(String productName) {
          this.productName = productName;
      }
  
      public float getPrice() {
          return totalPrice;
      }
  
      public void setPrice(float price) {
          this.price = price;
      }
  
      public float getVat() {
          return vat;
      }
  
      public void setVat(float vat) {
          this.vat = vat;
      }
  
      public Date getManufacturedDate() {
          return manufacturedDate;
      }
  
      public void setManufacturedDate(Date manufacturedDate) {
          this.manufacturedDate = manufacturedDate;
      }
  
      @Override
      public String toString() {
          return "Product [id=" + id + ", productCode=" + productCode
                  + ", productName=" + productName + ", price=" + price
                  + ", vat=" + vat + ", manufacturedDate=" + manufacturedDate + "]";
      }
  }
  </code></pre>
  
  <p><strong>ProductRepository.java</strong></p>
  
  <pre><code>public interface ProductRepository extends JpaRepository&lt;Product, Integer&gt;{
      Product findByProductCode(String productCode);
  }
  </code></pre>
  
  <p><strong>persistence.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="1.0"
      xmlns="http://java.sun.com/xml/ns/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_1_0.xsd"&gt;
  
      &lt;persistence-unit name="jpa-mysql-db" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;class&gt;com.javamakeuse.entity.Product&lt;/class&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>database-context.xml</strong></p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jdbc="http://www.springframework.org/schema/jdbc"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jpa="http://www.springframework.org/schema/data/jpa"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
      &lt;context:annotation-config/&gt;    
      &lt;context:component-scan base-package="com.javamakeuse.*"/&gt;
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
      &lt;context:property-placeholder location="classpath:database.properties" /&gt;
  
      &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/&gt;
  
      &lt;!-- Jpa Repositories --&gt;
      &lt;jpa:repositories base-package="com.javamakeuse.repository" /&gt;
  
      &lt;bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="${mysql.driver.class.name}" /&gt;
          &lt;property name="url" value="${mysql.url}" /&gt;
          &lt;property name="username" value="${mysql.username}" /&gt;
          &lt;property name="password" value="${mysql.username}" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
          &lt;property name="showSql" value="true" /&gt;
          &lt;property name="generateDdl" value="${mysql.generate.ddl}" /&gt;
          &lt;property name="databasePlatform" value="${mysql.dialect}" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- ================= Entity Manager ===================== --&gt;
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="datasource" /&gt;
  
          &lt;property name="packagesToScan" value="com.scheduler.model" /&gt;
  
          &lt;property name="jpaVendorAdapter" ref="jpaVendorAdapter"/&gt;
  
          &lt;property name="persistenceUnitName" value="${mysq.persistent.unit.name}" /&gt;
  
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                &lt;prop key="hibernate.show_sql"&gt;${hibernate.show.sql}&lt;/prop&gt;
                &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- ==================== Transaction Manager ========================== --&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>database.properties</strong></p>
  
  <pre><code>mysql.driver.class.name=com.mysql.jdbc.Driver
  mysql.url=jdbc:mysql://localhost/test
  mysql.username=root
  mysql.password=root
  mysql.dialect=org.hibernate.dialect.MySQLDialect
  
  hibernate.show.sql=true
  hibernate.hbm2ddl.auto=update
  mysq.persistent.unit.name=jpa-mysql-db
  mysql.generate.ddl=true
  </code></pre>
  
  <p><strong>db.sql</strong></p>
  
  <pre><code>create table product (
      product_id integer not null auto_increment, 
      manufactured_date datetime, 
      price float, 
      product_code varchar(255), 
      product_name varchar(255), 
      vat float, 
      primary key (product_id)
  );
  
  INSERT INTO product (manufactured_date,price,product_code,product_name,vat) VALUES (STR_TO_DATE('03-03-2016', '%d-%m-%Y'), 130, 'AZ00010','IPhone-New-7', 5.6);
  INSERT INTO product (manufactured_date,price,product_code,product_name,vat) VALUES (STR_TO_DATE('01-01-2016', '%d-%m-%Y'), 130, 'AZ00011','Lenovo Vibe X3', 5.6);
  </code></pre>
  
  <p><strong>application-context.xml</strong></p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jpa="http://www.springframework.org/schema/data/jpa"
      xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"&gt;
  
      &lt;import resource="database-context.xml"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>ProductTest.java</strong></p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = "classpath:application-context.xml")
  @Transactional(propagation = Propagation.REQUIRED)
  @Rollback
  public class ProductTest {
      private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
      private SimpleDateFormat SDF = new SimpleDateFormat(DATE_FORMAT);
  
      @PersistenceContext
      private EntityManager em;
  
      @Autowired
      private ProductRepository productRepository; 
  
      @Test
      public void testProductFinal() throws ParseException {
          Product product = new Product();
          product.setManufacturedDate(SDF.parse("2016-08-08 01:01:01"));
          product.setPrice(200);
          product.setProductCode("AZ0100");
          product.setProductName("Lenovo Vibe X3");
          product.setVat(5.64F);
  
          productRepository.save(product);
      }
  
      @Test
      public void testFindByProductCode(){
          Product product = productRepository.findByProductCode("AZ00010");
          System.out.println("---------------------");
          System.out.println("ManufacturedDate  : "+product.getManufacturedDate());
          System.out.println("Price             : "+product.getPrice());
          System.out.println("ProductCode       : "+product.getProductCode());
          System.out.println("Product Name      : "+product.getProductName());
          System.out.println("Vat               : "+product.getVat());
      }
  }
  </code></pre>
  
  <p>The below logger is saying that entity is inserted, but its not getting persisted into DB. Please help. I'm using MYSQL DB.</p>
  
  <pre><code>Hibernate: insert into product (manufactured_date, price, product_code, product_name, vat) values (?, ?, ?, ?, ?)
  </code></pre>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>&lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;spring.version&gt;4.3.1.RELEASE&lt;/spring.version&gt;
          &lt;spring-data-jpa-version&gt;1.9.2.RELEASE&lt;/spring-data-jpa-version&gt;
          &lt;hibernate-entitymanager-version&gt;5.1.0.Final&lt;/hibernate-entitymanager-version&gt;
          &lt;mysql.version&gt;5.1.39&lt;/mysql.version&gt;
          &lt;querydsl.version&gt;4.1.3&lt;/querydsl.version&gt;
          &lt;camel.version&gt;2.17.3&lt;/camel.version&gt;
          &lt;logback.version&gt;1.1.7&lt;/logback.version&gt;
          &lt;jcl-over-slf4j.version&gt;1.7.21&lt;/jcl-over-slf4j.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Spring TEST --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&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;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Spring Data JPA --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
              &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
              &lt;version&gt;${spring-data-jpa-version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Hibernate Entity Manager --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;${hibernate-entitymanager-version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt;
              &lt;version&gt;1.0.0.Final&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- QueryDSL --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.querydsl&lt;/groupId&gt;
              &lt;artifactId&gt;querydsl-jpa&lt;/artifactId&gt;
              &lt;version&gt;${querydsl.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.querydsl&lt;/groupId&gt;
              &lt;artifactId&gt;querydsl-apt&lt;/artifactId&gt;
              &lt;version&gt;${querydsl.version}&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
  
          &lt;!-- Junit --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.12&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- MYSQL --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;${mysql.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- logging Framework Logback, slf4j --&gt;
          &lt;!-- &lt;dependency&gt; &lt;groupId&gt;org.slf4j&lt;/groupId&gt; &lt;artifactId&gt;jcl-over-slf4j&lt;/artifactId&gt; 
              &lt;version&gt;${jcl-over-slf4j.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt; 
              &lt;artifactId&gt;logback-classic&lt;/artifactId&gt; &lt;version&gt;${logback.version}&lt;/version&gt; 
              &lt;/dependency&gt; --&gt;
      &lt;/dependencies&gt;
  </code></pre>