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

0

Issue

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;

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

Application.properties for Sring Boot:

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

Solution

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

Example here : http://www.java2s.com/Code/Java/JPA/SetInitialValueOfTableGenerator.htm

Answered By – Ramachandran.A.G

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More