如何在 Spring JPA 中为 MySQL 设置 @Id @GeneratedValue 的初始值?

How to set initial value for @Id @GeneratedValue in Spring JPA for MySQL?

我不知道如何设置 @GenerateValue @Id 的初始值。

我试过使用 GenerationType.SEQUENCEMySQL 不允许这样做。如何设置要用于 @GenerateValue 的初始值?

同时使用 AUTOTABLE 除了 1

我仍然无法获得初始值

谢谢

代码 使用 AUTO

@Entity
@Data
@SequenceGenerator(name = "port_gen", sequenceName = "port_gen",  initialValue = 4700)
public class AppiumPort {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "port_gen")
    private Long port;

    public AppiumPort() {

    }
}

代码使用TABLE

    @Entity
    @Data
    public class AppiumPort {

    @TableGenerator(name = "Address_Gen", table = "ID_GEN", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "Addr_Gen", initialValue = 10000, allocationSize = 100)
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "Address_Gen")
    private int port;

    public AppiumPort() {

    }
}

** 更新 **

问题与未设置 hibernate.id.new_generator_mappings=true;

有关

https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/ch01.html

Application.properties 对于 Sring Boot:

spring.jpa.properties.hibernate.id.new_generator_mappings=true

您可以尝试使用@TableGenerator(JPA 有一个@TableGenerator 注释,您可以在其中设置初始值)。 initialValue 可用于播种值

此处示例:http://www.java2s.com/Code/Java/JPA/SetInitialValueOfTableGenerator.htm

这对我有用:

@Id
@GenericGenerator(
    name = "customers-sequence-generator",
    strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
    parameters = {
            @Parameter(name = "sequence_name", value = "customers_sequence"),
            @Parameter(name = "initial_value", value = "1000"),
            @Parameter(name = "increment_size", value = "1")
    })
@GeneratedValue(generator = "customers-sequence-generator")
@Column(nullable=false, updatable=false)
@JsonProperty("id")
private long id;