title: Why Spring Boot app entity id doesn't auto generate
body: <p>In the app Model:-</p>
  <pre><code>    @Entity
  @Table(name=&quot;TBL_EMPLOYEES&quot;)
  public class EmployeeEntity {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      
      @Column(name=&quot;first_name&quot;)
      private String firstName;
      
      @Column(name=&quot;last_name&quot;)
      private String lastName;
      
      @Column(name=&quot;email&quot;, nullable=false, length=200)
      private String email;
  
      public EmployeeEntity() {
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      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 getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      @Override
      public String toString() {
          return &quot;EmployeeEntity [id=&quot; + id + &quot;, firstName=&quot; + firstName + 
                  &quot;, lastName=&quot; + lastName + &quot;, email=&quot; + email   + &quot;]&quot;;
      }
  }
  </code></pre>
  <p>Controller:-</p>
  <pre><code>    @RestController
  @RequestMapping(&quot;/employees&quot;)
  public class EmployeeController {
      @Autowired
      EmployeeService service;
  
      @GetMapping
      public ResponseEntity&lt;List&lt;EmployeeEntity&gt;&gt; getAllEmployees() {
          List&lt;EmployeeEntity&gt; list = service.getAllEmployees();
  
          return new ResponseEntity&lt;List&lt;EmployeeEntity&gt;&gt;(list, new HttpHeaders(), HttpStatus.OK);
      }
  
      @GetMapping(&quot;/{id}&quot;)
      public ResponseEntity&lt;EmployeeEntity&gt; getEmployeeById(@PathVariable(&quot;id&quot;) Long id)
              throws RecordNotFoundException {
          EmployeeEntity entity = service.getEmployeeById(id);
  
          return new ResponseEntity&lt;EmployeeEntity&gt;(entity, new HttpHeaders(), HttpStatus.OK);
      }
  
      @PostMapping
      public ResponseEntity&lt;EmployeeEntity&gt; createOrUpdateEmployee(@RequestBody EmployeeEntity employee)
              throws RecordNotFoundException {
          EmployeeEntity updated = service.createOrUpdateEmployee(employee);
          return new ResponseEntity&lt;EmployeeEntity&gt;(updated, new HttpHeaders(), HttpStatus.OK);
      }
  
      @DeleteMapping(&quot;/{id}&quot;)
      public HttpStatus deleteEmployeeById(@PathVariable(&quot;id&quot;) Long id)
              throws RecordNotFoundException {
          service.deleteEmployeeById(id);
          return HttpStatus.OK;
      }
  
  }
  </code></pre>
  <p>When I tried to test by curl POST request as:-</p>
  <p>curl --location --request POST 'localhost:8080 /employees' <br />
  --header 'Content-Type: application/json' <br />
  --data-raw '{</p>
  <pre><code>&quot;firstName&quot;: &quot;firstName&quot;,
  &quot;lastName&quot;: &quot;lastName&quot;,
  &quot;email&quot;: &quot;xxx@test.com&quot;
  </code></pre>
  <p>}'</p>
  <p>I got error as:-</p>
  <p>2020-07-21 14:00:18.938 ERROR 3740 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!] with root cause</p>
  <p>java.lang.IllegalArgumentException: The given id must not be null!</p>
  <p>If I pass id then it works. Why id doesn't auto generated? How to fix this issue?</p>
  <p><strong>More code:</strong></p>
  <p>application.properties:-</p>
  <pre><code>spring.datasource.url=jdbc:h2:c:/temp/tempdb
  spring.datasource.driverClassName=org.h2.Driver
  spring.datasource.username=sa
  spring.datasource.password=
  spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
  # Enabling H2 Console
  spring.h2.console.enabled=true
  # Custom H2 Console URL
  spring.h2.console.path=/h2
  # Hibernate ddl auto (create, create-drop, validate, update)
  spring.jpa.hibernate.ddl-auto=update
  # Show all controller mapping
  logging.level.org.springframework.web.servlet.mvc.method.annotation=trace
  #Turn Statistics on and log SQL stmts
  spring.jpa.properties.hibernate.format_sql=true
  #If want to see very extensive logging
  spring.jpa.show-sql=true
  spring.jpa.properties.hibernate.generate_statistics=true
  logging.level.org.hibernate.type=trace
  logging.level.org.hibernate.stat=debug
  log4j.category.org.springframework.web=INFO
  logging.level.org.hibernate.SQL=DEBUG
  </code></pre>
  <p>In the resource folder I have:-</p>
  <p>Service:-</p>
  <pre><code>@Service
  public class EmployeeService {
  
  @Autowired
  EmployeeRepository repository;
  
  public List&lt;EmployeeEntity&gt; getAllEmployees() {
      List&lt;EmployeeEntity&gt; employeeList = repository.findAll();
  
      if (employeeList.size() &gt; 0) {
          return employeeList;
      } else {
          return new ArrayList&lt;EmployeeEntity&gt;();
      }
  }
  
  public EmployeeEntity getEmployeeById(Long id) throws RecordNotFoundException {
      Optional&lt;EmployeeEntity&gt; employee = repository.findById(id);
  
      if (employee.isPresent()) {
          return employee.get();
      } else {
          throw new RecordNotFoundException(&quot;No employee record exist for given id&quot;);
      }
  }
  
  public EmployeeEntity createOrUpdateEmployee(EmployeeEntity entity) throws RecordNotFoundException {
      return repository.save(entity);
  }
  
  public void deleteEmployeeById(Long id) throws RecordNotFoundException {
      Optional&lt;EmployeeEntity&gt; employee = repository.findById(id);
  
      if (employee.isPresent()) {
          repository.deleteById(id);
      } else {
          throw new RecordNotFoundException(&quot;No employee record exist for given id&quot;);
      }
  }
  </code></pre>
  <p>}</p>
  <p>schema.sql:-</p>
  <pre><code>DROP TABLE IF EXISTS TBL_EMPLOYEES;
  
   CREATE TABLE TBL_EMPLOYEES (
   id INT AUTO_INCREMENT  PRIMARY KEY,
   first_name VARCHAR(250) NOT NULL,
   last_name VARCHAR(250) NOT NULL,
   email VARCHAR(250) DEFAULT NULL
   );
  </code></pre>
  <p>data.sql:-</p>
  <pre><code>INSERT INTO 
  TBL_EMPLOYEES (first_name, last_name, email) 
  VALUES
  ('Lokesh', 'Gupta', 'howtodoinjava@gmail.com'),
  ('John', 'Doe', 'xyz@email.com');
  </code></pre>