jpa mysql id初始值_如何在Spring JPA for MySQL中为@Id @GeneratedValue设置初始值?

在使用Spring JPA与MySQL时,遇到为@Id @GeneratedValue字段设置初始值的问题。尝试了GenerationType.SEQUENCE但在MySQL中不支持。使用AUTO和TABLE策略也无法将初始值设置为非1的数值。解决方案是在application.properties中添加spring.jpa.properties.hibernate.id.new_generator_mappings=true。
摘要由CSDN通过智能技术生成

bd96500e110b49cbb3cd949968f18be7.png

I can not figure out how to set the initial value for the @GenerateValue @Id.

I have tried using GenerationType.SEQUENCE but that is not allowed in MySQL. How can I set the initial value to be used for the @GenerateValue?

Using both AUTO and TABLE I can still not get the initial value to start at anything but 1

Thank you

Code using 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() {

}

}

Code using 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() {

}

}

** UPDATE **

The problem was related to not setting hibernate.id.new_generator_mappings=true;

Application.properties for Sring Boot:

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

解决方案

You could try and use the @TableGenerator (JPA has a @TableGenerator annotation in which you can set an initial value). The initialValue can be used to seed the values

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值