如何在Spring JPA for MySQL中为@Id @GeneratedValue设置初始值? [英] How to set initial value for @Id @GeneratedValue in Spring JPA for MySQL?

查看:350
本文介绍了如何在Spring JPA for MySQL中为@Id @GeneratedValue设置初始值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我尝试使用GenerationType.SEQUENCE,但是在MySQL中不允许这样做.如何设置用于@GenerateValue的初始值?

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?

同时使用AUTOTABLE,我仍然无法获得除1

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

谢谢

代码,使用自动

@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;

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

https://docs.jboss.org/hibernate/stable/annotations/reference/zh-CN/html/ch01.html

用于Sring Boot的Application.properties:

Application.properties for Sring Boot:

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

推荐答案

您可以尝试使用@TableGenerator(JPA具有@TableGenerator批注,您可以在其中设置初始值). initialValue可以用来播种值

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

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

这篇关于如何在Spring JPA for MySQL中为@Id @GeneratedValue设置初始值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆