title: Issue in H2 with GenerationType.IDENTITY & schema.sql -- SpringBoot
body: <p>I am trying to create and preload <strong>embedded H2 Db</strong> with some data using <strong>schema.sql</strong>. But once I try to add new user using POST request, the following error occurs.</p>
  <blockquote>
  <p>2022-07-04 23:35:53.153 ERROR 8180 --- [nio-8080-exec-2]
  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.DataIntegrityViolationException: could not
  execute statement; SQL [n/a]; constraint [&quot;PRIMARY KEY ON
  PUBLIC.USERS(USER_ID) ( /* key:1 */ 1, 'mrxxx', 'mrxxx@gmail.com',
  '123456')&quot;; SQL statement: insert into users (user_id, email,
  user_name, password) values (default, ?, ?, ?) [23505-214]]; nested
  exception is org.hibernate.exception.ConstraintViolationException:
  could not execute statement] with root cause</p>
  <p>org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index
  or primary key violation: &quot;PRIMARY KEY ON PUBLIC.USERS(USER_ID) ( /*
  key:1 */ 1, 'mrxxx', 'mrxxx@gmail.com', '123456')&quot;; SQL statement:
  insert into users (user_id, email, user_name, password) values
  (default, ?, ?, ?) [23505-214]</p>
  </blockquote>
  <p>I guess this is happening because the <strong>@GeneratedValue(strategy = GenerationType.IDENTITY)</strong> is trying to create user_id = 1 but it already exists in Db as we have preloaded it. I dont know how to get rid of it</p>
  <p><em>Request</em></p>
  <pre><code>{
      &quot;name&quot; : &quot;Arunkumar&quot;,
      &quot;email&quot; : &quot;arunkavi@gmail.com&quot;,
      &quot;password&quot; : &quot;arun1234&quot; 
  }
  </code></pre>
  <p><em>Response</em></p>
  <pre><code>{
      &quot;timestamp&quot;: &quot;2022-07-04T18:05:53.184+00:00&quot;,
      &quot;status&quot;: 500,
      &quot;error&quot;: &quot;Internal Server Error&quot;,
      &quot;path&quot;: &quot;/post&quot;
  }
  </code></pre>
  <p>Please find the code snippets below</p>
  <p><em>application.properties</em></p>
  <pre><code>spring.jpa.hibernate.ddl-auto=none
  spring.datasource.initialization-mode=always
  
  spring.h2.console.enabled=true
  spring.h2.console.path=/h2
  spring.datasource.url=jdbc:h2:mem:testdb
  spring.datasource.driver-class-name= org.h2.Driver
  spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
  </code></pre>
  <p><em>schema.sql</em></p>
  <pre><code>DROP TABLE IF EXISTS `users`;
  
  CREATE TABLE `users` (
    `user_id` int NOT NULL AUTO_INCREMENT,
    `user_name` varchar(45) DEFAULT NULL,
    `email` varchar(45) DEFAULT NULL,
    `password` varchar(45) DEFAULT NULL,
    PRIMARY KEY (`user_id`)
  );
  
  INSERT INTO `users` VALUES (1,'mrxxx', 'mrxxx@gmail.com', '123456');
  </code></pre>
  <p><em>Users.java</em></p>
  <pre><code>@Entity
  public class Users {    
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name=&quot;USER_ID&quot;)
      private int id;
      @Column(name=&quot;USER_NAME&quot;)
      private String name;
      @Column(name=&quot;PASSWORD&quot;)
      private String password;
      @Column(name=&quot;EMAIL&quot;)
      private String email;
      .......
  }
  </code></pre>
  <p><em>DbCotroller.java</em></p>
  <pre><code>@RestController
  public class DbController {
  
      @Autowired
      private DbRepo userRepo;
      
      @PostMapping(&quot;/post&quot;)
      public ResponseEntity&lt;?&gt; saveUser(@RequestBody Users user){
          userRepo.save(user);
          return new ResponseEntity&lt;&gt;(user, HttpStatus.CREATED);
      }
  }
  </code></pre>