title: Spring + JPA + Mysql The database returned no natively generated identity value
body: <p>I am a beginner for Spring and I tried a simple java-mysql persistence example.</p>
  
  <h1>pom.xml</h1>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.example&lt;/groupId&gt;
      &lt;artifactId&gt;mysqlConnect&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;mysqlConnect&lt;/name&gt;
      &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.4.3.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;!--&lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;--&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  
  &lt;/project&gt;
  </code></pre>
  
  <h1>application.properties</h1>
  
  <pre><code>spring.datasource.url=jdbc:mysql://localhost:3306/demo
  spring.datasource.username=user
  spring.datasource.password=password
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  </code></pre>
  
  <h1>Student.java</h1>
  
  <pre><code>package com.example.models;
  
  import javax.persistence.*;
  
  @Entity
  @Table(name = "Student")
  public class Student {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
      private String name;
      private String town;
  
      protected Student() {
      }
  
      public Student(String name, String town) {
          this.name = name;
          this.town = town;
      }
  
      @Column(name = "id")
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      @Column(name = "name")
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @Column(name = "town")
      public String getTown() {
          return town;
      }
  
      public void setTown(String town) {
          this.town = town;
      }
  
      @Override
      public String toString() {
          return "Student{" +
                  "town='" + town + '\'' +
                  ", name='" + name + '\'' +
                  '}';
      }
  }
  </code></pre>
  
  <h1>StudnetsServices.java</h1>
  
  <pre><code>package com.example.services;
  
  import com.example.models.Student;
  import org.springframework.data.repository.CrudRepository;
  
  import java.util.List;
  
  public interface StudentsServices extends CrudRepository&lt;Student, Long&gt; {
          List&lt;Student&gt; findByName(String name);
  }
  </code></pre>
  
  <h1>application.java</h1>
  
  <pre><code>package com.example;
  
  import com.example.models.Student;
  import com.example.services.StudentsServices;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.annotation.Bean;
  
  @SpringBootApplication
  public class MysqlConnectApplication {
  
      private static final Logger log = LoggerFactory.getLogger(MysqlConnectApplication.class);
  
      public static void main(String[] args) {
          SpringApplication.run(MysqlConnectApplication.class, args);
      }
  
      @Bean
      public CommandLineRunner demo(StudentsServices repository){
          return args -&gt; {
              log.info("Saving Tom");
              repository.save(new Student("Tom", "London"));
              log.info("****************************************************");
              log.info("Saving Jerry");
              repository.save(new Student("Jerry", "New-york"));
              log.info("****************************************************");
              log.info("Reeding All");
              for (Student student : repository.findAll()){log.info(student.toString());}
              log.info("****************************************************");
              log.info("Found by name");
              for (Student student : repository.findByName("Thuwarakesh")){
                  log.info(student.toString());
              }
              log.info("****************************************************");
  
          };
      }
  }
  </code></pre>
  
  <h1>Table in Mysql</h1>
  
  <pre><code>CREATE TABLE IF NOT EXISTS Student(
    id INT AUTO_INCREMENT,
    name VARCHAR(25),
    town VARCHAR(26),
    PRIMARY KEY (id)
  );
  </code></pre>
  
  <p>When I compline this and run the jar file, I am getting the bellow exception.</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to execute CommandLineRunner
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:803) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
      at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
      at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:771) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
      at com.example.MysqlConnectApplication.main(MysqlConnectApplication.java:18) [classes!/:0.0.1-SNAPSHOT]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
      at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
      at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [mysqlConnect-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
      at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [mysqlConnect-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
      at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [mysqlConnect-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
      at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [mysqlConnect-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
  Caused by: org.springframework.orm.jpa.JpaSystemException: The database returned no natively generated identity value; nested exception is org.hibernate.HibernateException: The database returned no natively generated identity value
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333) ~[spring-orm-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244) ~[spring-orm-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491) ~[spring-orm-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.10.6.RELEASE.jar!/:na]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at com.sun.proxy.$Proxy59.save(Unknown Source) ~[na:na]
      at com.example.MysqlConnectApplication.lambda$demo$0(MysqlConnectApplication.java:25) [classes!/:0.0.1-SNAPSHOT]
      at com.example.MysqlConnectApplication$$Lambda$7/889729797.run(Unknown Source) ~[na:na]
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
      ... 14 common frames omitted
  Caused by: org.hibernate.HibernateException: The database returned no natively generated identity value
      at org.hibernate.id.IdentifierGeneratorHelper.getGeneratedIdentity(IdentifierGeneratorHelper.java:71) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:61) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2803) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3374) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:619) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:273) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:254) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:299) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) ~[hibernate-entitymanager-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753) ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146) ~[hibernate-entitymanager-5.0.11.Final.jar!/:5.0.11.Final]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
      at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298) ~[spring-orm-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at com.sun.proxy.$Proxy57.persist(Unknown Source) ~[na:na]
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:506) ~[spring-data-jpa-1.10.6.RELEASE.jar!/:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
      at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503) ~[spring-data-commons-1.12.6.RELEASE.jar!/:na]
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488) ~[spring-data-commons-1.12.6.RELEASE.jar!/:na]
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460) ~[spring-data-commons-1.12.6.RELEASE.jar!/:na]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.12.6.RELEASE.jar!/:na]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
      ... 24 common frames omitted
  </code></pre>