Skip to content
Advertisement

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

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

JavaScript

Code using TABLE

JavaScript

** 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:

JavaScript

Advertisement

Answer

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

Advertisement