title: @SequenceGenerator throws After saving the identifier must not be null! when value of id not passed in h2
body: <p>I am using spring boot with h2 database when using a sequence and using @SequenceGenerator doesn't seem to be generating the value for the primary key. I want to generate the value of Id by default and then update the values into the database.
  Here are the details of my class.</p>
  <p>CardHolder.java</p>
  <pre><code>package com.spring.book.rental.dao.model;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.SequenceGenerator;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = &quot;card_holder&quot;)
  public class CardHolder {
  
      @Id
      @Column
        @GeneratedValue(generator=&quot;SEQ_CARD_HOLDER_ID&quot;,strategy=GenerationType.IDENTITY)
        @SequenceGenerator(name=&quot;SEQ_CARD_HOLDER_ID&quot;,
        sequenceName=&quot;SEQ_PG_CARD_HOLDER_ID&quot;,initialValue = 1)
      public int id;
  
      @Column(name = &quot;last_name&quot;)
      public String lastName;
  
      @Column(name = &quot;first_name&quot;)
      public String firstName;
  
      @Column(name = &quot;card_number&quot;)
      public String cardNumber;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      public String getCardNumber() {
          return cardNumber;
      }
  
      public void setCardNumber(String cardNumber) {
          this.cardNumber = cardNumber;
      }
  }
  </code></pre>
  <p>BookController.java</p>
  <pre><code>package com.spring.book.rental.controller;
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.spring.book.rental.DTO.BookDTO;
  import com.spring.book.rental.DTO.RentBookRequest;
  import com.spring.book.rental.service.BookService;
  
  @RestController
  public class BookController {
  
      @Autowired
      BookService bookService;
      
      @PostMapping(path=&quot;book-rental/rentABook&quot;, consumes = &quot;application/json&quot;)
      public String rentABook(@RequestBody RentBookRequest bookRequest) {
          try {
              bookService.rentBook(bookRequest);
              return &quot;You rented a Book Id : &quot;+bookRequest.getBookId();
          } catch (Exception e) {
              return e.getMessage();
          }
      }
      
  }
  </code></pre>
  <p>BookServiceImpl.java</p>
  <pre><code>package com.spring.book.rental.service.impl;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.springframework.beans.BeanUtils;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Service;
  
  import com.spring.book.rental.DTO.BookDTO;
  import com.spring.book.rental.DTO.RentBookRequest;
  import com.spring.book.rental.dao.BookDAO;
  import com.spring.book.rental.dao.CardHolderDAO;
  import com.spring.book.rental.dao.RentalDAO;
  import com.spring.book.rental.dao.model.Book;
  import com.spring.book.rental.dao.model.CardHolder;
  import com.spring.book.rental.dao.model.Rental;
  import com.spring.book.rental.service.BookService;
  
  @Service(&quot;bookService&quot;)
  @Component
  public class BookServiceImpl implements BookService {
  
      @Autowired
      BookDAO bookDao;
      
      @Autowired
      CardHolderDAO cardHolderDAO;
      
      @Autowired
      RentalDAO rentalDAO;
  
      @Override
      public void rentBook(RentBookRequest bookRequest) throws Exception {
                      
          CardHolder cardHolder = new CardHolder();
          cardHolder.setFirstName(bookRequest.getFirstName());
          cardHolder.setLastName(bookRequest.getLastName());
          cardHolder.setCardNumber(bookRequest.getCardNumber());
          cardHolderDAO.addCardHolder(cardHolder);
          
          /* Rental rental = new Rental();
          rental.setBook_id(bookRequest.getBookId());
          rental.setCardHolder_id(cardHolder.getId());
          rentalDAO.addRental(rental); */
          
      }
  
  }
  </code></pre>
  <p>CardHolderDAOImpl.java</p>
  <pre><code>package com.spring.book.rental.dao.impl;
  
  import java.math.BigDecimal;
  import java.util.List;
  import java.util.Optional;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.dao.DataAccessException;
  import org.springframework.stereotype.Repository;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  import com.spring.book.rental.dao.CardHolderDAO;
  import com.spring.book.rental.dao.model.CardHolder;
  import com.spring.book.rental.dao.repository.CardHolderRepository;
  
  @Repository
  public class CardHolderDAOImpl implements CardHolderDAO {
  
      @Autowired
      CardHolderRepository cardHolderRepository;
      
      @Override
      public void addCardHolder(CardHolder cardHolder) {
          cardHolderRepository.save(cardHolder);
      }
      
  }
  </code></pre>
  <p>CardHolderRepository.java</p>
  <pre><code>package com.spring.book.rental.dao.repository;
  
  import java.util.Optional;
  
  import org.springframework.data.repository.CrudRepository;
  
  import com.spring.book.rental.dao.model.CardHolder;
  
  public interface CardHolderRepository extends CrudRepository&lt;CardHolder, Integer&gt; {
  
      public Optional&lt;CardHolder&gt; findById(Integer id);
  }
  </code></pre>
  <p>RentBookRequest.java</p>
  <pre><code>package com.spring.book.rental.DTO;
  
  public class RentBookRequest {
  
      private int bookId;
      
      private int cardHolderId;
      
      private String firstName;
      
      private String lastName;
      
      private String cardNumber;
  
      public int getBookId() {
          return bookId;
      }
  
      public void setBookId(int bookId) {
          this.bookId = bookId;
      }
  
      public int getCardHolderId() {
          return cardHolderId;
      }
  
      public void setCardHolderId(int cardHolderId) {
          this.cardHolderId = cardHolderId;
      }
  
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
      public String getCardNumber() {
          return cardNumber;
      }
  
      public void setCardNumber(String cardNumber) {
          this.cardNumber = cardNumber;
      }
      
  }
  </code></pre>
  <p>Here is the main class</p>
  <p>SpringTaskBookRentalApplication.java</p>
  <pre><code>package com.spring.book.rental;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.annotation.ComponentScan;
  
  import sun.misc.Contended;
  
  @ComponentScan(basePackages = &quot;com&quot;)
  @EnableAutoConfiguration
  @SpringBootApplication
  public class SpringTaskBookRentalApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(SpringTaskBookRentalApplication.class, args);
      }
  
  }
  </code></pre>
  <p>I have added this in the application.properties file</p>
  <pre><code>spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
  spring.datasource.driverClassName=org.h2.Driver
  spring.datasource.url=jdbc:h2:mem:bookrentaldb
  spring.datasource.username=admin
  spring.datasource.password=admin
  spring.h2.console.enabled=true
  spring.h2.console.path=/h2
  
  spring.datasource.continue-on-error=false
  spring.datasource.initialize=true
  spring.datasource.separator=;
  spring.datasource.sql-script-encoding=UTF-8
  </code></pre>
  <p>Here is the schema.sql file</p>
  <pre><code>create table BOOK(
      id int not null primary key,
      title varchar(50) not null,
      IS_BOOK_AVAILABLE boolean not null,
      author varchar(50) not null,
      primary key(id)
  );
  
  create table card_holder (
      id int not null primary key AUTO_INCREMENT,
      first_name varchar(20),
      last_name varchar(20),
      card_number varchar(20),
      primary key(id)
  );
  
  create table Rental(
      rental_id int not null primary key AUTO_INCREMENT,
      cardHolder_id int not null,
      book_id int not null
  );
  
   CREATE SEQUENCE SEQ_PG_CARD_HOLDER_ID  MINVALUE 1 MAXVALUE 9999 INCREMENT BY 1 START WITH 1  NOCYCLE ;
   
   CREATE SEQUENCE SEQ_PG_Rental_ID  MINVALUE 1 MAXVALUE 999999 INCREMENT BY 1 START WITH 100   NOCYCLE ;
  </code></pre>
  <p>Here is the data.sql</p>
  <pre><code>insert into BOOK (id,title,IS_BOOK_AVAILABLE,author) values (101,'A Thousand Splendid Suns','true','Khalid Hosseini');
  
  insert into BOOK (id,title,IS_BOOK_AVAILABLE,author) values (102,'Head First Design Patterns','true','Elisabeth Freeman and Kathy Sierra');
  
  insert into BOOK (id,title,IS_BOOK_AVAILABLE,author) values (103,'Head First Java','true','Book by Bert Bates and Kathy Sierra');
  
  commit;
  </code></pre>
  <p>I have tried several other things with @GeneratedValue</p>
  <pre><code>@Id
      @Column
      @GeneratedValue(strategy=GenerationType.AUTO)
      public int id;
  </code></pre>
  <p>This is the stackTrace of error I am getting</p>
  <pre><code>java.lang.IllegalArgumentException: After saving the identifier must not be null!
      at org.springframework.util.Assert.notNull(Assert.java:198)
      at org.springframework.data.jdbc.core.JdbcAggregateTemplate.store(JdbcAggregateTemplate.java:343)
      at org.springframework.data.jdbc.core.JdbcAggregateTemplate.save(JdbcAggregateTemplate.java:149)
      at org.springframework.data.jdbc.repository.support.SimpleJdbcRepository.save(SimpleJdbcRepository.java:55)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.springframework.data.repository.core.support.ImplementationInvocationMetadata.invoke(ImplementationInvocationMetadata.java:72)
      at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:382)
      at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:205)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:549)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:155)
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
      at com.sun.proxy.$Proxy81.save(Unknown Source)
      at com.spring.book.rental.dao.impl.CardHolderDAOImpl.addCardHolder(CardHolderDAOImpl.java:38)
      at com.spring.book.rental.dao.impl.CardHolderDAOImpl$$FastClassBySpringCGLIB$$8e01a85d.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
      at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
      at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
      at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
      at com.spring.book.rental.dao.impl.CardHolderDAOImpl$$EnhancerBySpringCGLIB$$5a6f1054.addCardHolder(&lt;generated&gt;)
      at com.spring.book.rental.service.impl.BookServiceImpl.rentBook(BookServiceImpl.java:68)
      at com.spring.book.rental.controller.BookController.rentABook(BookController.java:29)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:748)
  </code></pre>