Don Jose
Don Jose

Reputation: 1588

How to give Jpa @GeneratedValue(strategy = GenerationType.AUTO) a start value instead of 0

I have an entity that is supposed to get an id from the database automatically. I use MySQL so I would expect annotating that @GeneratedValue(strategy=GenerationType.AUTO) When i run the project i will import hardcoded master data needed for my application, but when i add a new data to same table using jpa it starts with id 0 thus saying id already exist. I have to make jpa start auto increment from the last id i have added . Is this possible

Upvotes: 1

Views: 5527

Answers (2)

Saurabh Oza
Saurabh Oza

Reputation: 179

We can use @SequenceGenerator to specify the start sequence.

@Entity
@Table(name = "employee_data")
@SequenceGenerator(name="employee_id_seq", initialValue=5, allocationSize=100)
public class EmployeeDO {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="employee_id_seq")
    private Long id;

Upvotes: 0

Prashant Katara
Prashant Katara

Reputation: 105

Use the GenerationType.TABLE approach here in order to feed the data based on what value already present in your database.

Upvotes: 1

Related Questions