Search code examples
javamysqlspringspring-bootspring-data-jpa

Weird autoincrement issue on Spring Boot 3.3.0 with MySQL 8


I am using one of the Spring Starter projects (gs-accessing-data-mysql-complete) and inserting three User objects through Postman

http://localhost:8080/demo/add?name=paco&[email protected]

I am getting an increment of 50 in the user_seq table only for the first two objects (??), but then no more increments happen and even more: the sequence is not used at all, it seems as if the MySQL autoincrement native feature is

Hibernate: select next_val as id_val from user_seq for update
Hibernate: update user_seq set next_val= ? where next_val=?
Hibernate: insert into user (email,name,id) values (?,?,?)
Hibernate: select next_val as id_val from user_seq for update
Hibernate: update user_seq set next_val= ? where next_val=?
Hibernate: insert into user (email,name,id) values (?,?,?)
Hibernate: insert into user (email,name,id) values (?,?,?)
Hibernate: insert into user (email,name,id) values (?,?,?)

Do you have any idea on what's is going on? is the user_seq mechanism semi-deprecated or something?

The code is this, just called from PostMan 3 times

@PostMapping(path="/add") // Map ONLY POST Requests
public @ResponseBody String addNewUser (@RequestParam String name
            , @RequestParam String email) {
    // @ResponseBody means the returned String is the response, not a view name
    // @RequestParam means it is a parameter from the GET or POST request
    User n = new User();
    n.setName(name);
    n.setEmail(email);
    userRepository.save(n);
    return "Saved";
}

Solution

  • With hibernate's strategy=GenerationType.AUTO mySQL DB uses a table based sequence strategy. This is a feature to increase by 50 and cache the ids to reuse.

    Use strategy=GenerationType.IDENTITY

    for more details read this.

    Generated value strategy AUTO