简体   繁体   中英

SpringBoot-Hibernate-Mysql id primary key value sequence is shared in all of my domain using @GeneratedValue

I have two classes/domains ( Person ) and ( Account ), they both have id field with @GeneratedValue(strategy = GeneratedType.AUTO) or any other GeneratedType , but saving domains cause them to share the sequence of Primary key.

When I save a person domain, it will use id=1 , then saving a account will use id=2 even though they are completely different domain, I don't want this, been searching for quite a while but I don't know the keyword to search.

EDIT: Changed Pet to Account

Im using Spring Boot 2.0.4.RELEASE with spring-boot-starter-data-jpa.

MySql is 8.0

I have a mappedSuperclass Domain Class, but even though I put Id separately per class, it still same behavior..

Domain Class

package com.myband.band.domain;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class Domain {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private int version;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

}

Person Class

package com.myband.band.domain;

import javax.persistence.Entity;

@Entity
public class Person extends Domain {

    private String firstName;

    private String lastName;

    private String nickName;

    private int age;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

Account Class

package com.myband.login.domain;

import javax.persistence.Entity;

import com.myband.band.domain.Domain;

@Entity
public class Account extends Domain {

    private String username;

    public Account() {
    }

    public Account(String username) {
        super();
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

When you are using @GeneratedValue(strategy = GenerationType.AUTO) , your table is not created with the auto increment feature in mysql. If you want to use that feature for the primary key, use @GeneratedValue(strategy = GenerationType.IDENTITY) instead.

Please refer Gergely Bacso 's answer to understand how GenerationType.AUTO works.

You can fix this by changing your ID definition to:

@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pet_generator") @SequenceGenerator(name="pet_generator", sequenceName = "pet_seq")

What currently happening to you is:
You defined "AUTO" ID generation logic, this has defaulted to using DB sequences, but because you have not specified any sequence, Hibernate decided to use a single, common sequence , that is shared between all entity definitions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM