Sudhir Ojha
Sudhir Ojha

Reputation: 3305

Spring Boot @GeneratedValue(strategy = GenerationType.IDENTITY) not working

I have created an Entity Student and having following properties:

@Entity
public class Student {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long studentId;
   private String studentName;
   // Setters and getters
}

It is generating the studentId like 1,2,3 and so on,that means it is incrementing the value as expected.

Now i have dropped my database student_db and created once again with same name. And then restart my application.Now few things i noticed here:

Now the issue or i don't know what it is ?

I auto generated the studentId from where it is was on last time, i mean studentId is 4. I don't know why it is happening. My expected result is studentId should start from 1 does not matter how many time i dropped my database. This is happening both with @GeneratedValue(strategy = GenerationType.IDENTITY) and @GeneratedValue(strategy = GenerationType.AUTO)

I am using following:

Spring Boot 2.0.0 RELEASE
MySQL 5.7.23

In my application.properties file i have setted

spring.jpa.hibernate.ddl-auto = update

What i am missing here?

Upvotes: 2

Views: 14438

Answers (2)

please try

@GeneratedValue(
    strategy= GenerationType.AUTO,
    generator="native"
)
@GenericGenerator(
    name = "native",
    strategy = "native"
)
private Long id;

Upvotes: 1

Brother
Brother

Reputation: 2210

This is related with the MySQL itself. You can try to truncate the table to see if this problem goes away:

TRUNCATE TABLE table_name; 

And run the command after to reset the AUTO_INCREMENT:

ALTER TABLE table_name AUTO_INCREMENT = 1; 

Upvotes: 3

Related Questions