title: How to auto generate primary key ID properly with Hibernate inserting records
body: <p>I have a Member entity class whose primary key is defined as:</p>
  
  <pre><code>    @Id
      @GeneratedValue
      private Long id;
  </code></pre>
  
  <p>I pre-loaded two records into database with Hibernate when I starts the application:</p>
  
  <pre><code>insert into Member (id, name, email, phone_number) values (0, 'John Smith', 'john.smith@mailinator.com', '2125551212')
  insert into Member (id, name, email, phone_number) values (1, 'Mary Smith', 'mary.smith@mailinator.com', '2025551212') 
  </code></pre>
  
  <p>Now the MySql database has two records:</p>
  
  <pre><code>select * from Member;
  +----+---------------------------+------------+--------------+
  | id | email                     | name       | phone_number |
  +----+---------------------------+------------+--------------+
  |  0 | john.smith@mailinator.com | John Smith | 2125551212   |
  |  1 | mary.smith@mailinator.com | Mary Smith | 2025551212   |
  </code></pre>
  
  <p>+----+---------------------------+------------+--------------+</p>
  
  <p>Now, in my member registration page, when I submit a request to register a new member, I received this error message:</p>
  
  <pre><code>Caused by: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
  </code></pre>
  
  <p>I figured that it because the auto-generated key always starts with '1', but in the database, it already has two records, so the primary key '1' will be duplicate.</p>
  
  <p>My question is, how to create the primary key properly, given a number of existing records in the table? If I don't use the </p>
  
  <pre><code>@GeneratedValue
  </code></pre>
  
  <p>annotation, then I always have to find out what's the next key from the database table before insertion.</p>
  
  <p>Is there a best way to handle this situation, which seems very common? </p>
  
  <p>EDITED:
  As suggested, I used the Stragey:</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  </code></pre>
  
  <p>What's strange is that, if there are two existing records in the table as below:</p>
  
  <pre><code>select * from English;
  +----+------------+---------+--------+
  | id | sentence   | source  | status |
  +----+------------+---------+--------+
  |  1 | i love you | unknown |      0 |
  |  2 | i like you | unknown |      0 |
  +----+------------+---------+--------+
  </code></pre>
  
  <p>after I register a new record into the table, the new ID starts with 4, not 3, as below. </p>
  
  <pre><code>select * from English;
  +----+----------------+---------+--------+
  | id | sentence       | source  | status |
  +----+----------------+---------+--------+
  |  1 | i love you     | unknown |      0 |
  |  2 | i like you     | unknown |      0 |
  |  4 | I have a book. | Unknown |      0 |
  +----+----------------+---------+--------+
  3 rows in set (0.00 sec)
  </code></pre>
  
  <p>What might cause this? </p>