Question: I have Migrated the Springboot version from 1.4.3.RELEASE to 2.1.0.RELEASE in my project. After that CrudRepository.save() always throws org.hibernate.exception.ConstraintViolationException: could not execute statement.

This is what I can see in the Logs : 

```
o.h.e.j.s.SqlExceptionHelper[m: SQL Error: 1062, SQLState: 23000
o.h.e.j.s.SqlExceptionHelper[m: Duplicate entry '11' for key 'PRIMARY'
o.h.i.ExceptionMapperStandardImpl[m: HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement

```

This is the entity I am trying to save.

```
@Getter
@Setter
@Entity
@Table(name = "project_m")
public class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "name" , nullable = false)
    private String name;

    //other fields

}

```

Answer: 
What has changed from Springboot 1.4.3.RELEASE to 2.1.0.RELEASE is the internal **Hibernate** version from **5.0** to **5.3**.

And what has changed in this is the way the SequenceGenerator works, which is used in case the strategy is GenerationType.AUTO (as is in your case).
[![enter image description here](https://i.stack.imgur.com/CLTq9.png)](https://i.stack.imgur.com/CLTq9.png)

Link to [hibernate migration doc here](https://github.com/hibernate/hibernate-orm/blob/5.2/migration-guide.adoc).

More details on hibernate generation strategy [here](http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators-auto).

My guess is there are 2 parallel sessions inserting into this table, and both of them now share a local copy of the sequence number, which creates this collision. Not sure though!

My suggestion would be to change the strategy to GenerationType.SEQUENCE and try.
I got a workaround for this issue by setting a large value in `hibernate_sequence` table. I saw that the primary key value from the duplicate primary key error is generated from a table named `hibernate_sequence`.

When we set GenerationType.AUTO in the entity,Hibernate selects the generation strategy based on the Hibernate dialect. In older versions , Hibernate selected the `GenerationType.IDENTITY` as default for MySQL databases.

It now selects the `GenerationType.TABLE` which uses a database table to generate primary keys