Reputation: 1588
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
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
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