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
Asked
Active
Viewed 4,669 times
1

Don Jose
- 1,448
- 2
- 13
- 27
-
Ensure the imported data uses auto-increment or change the value after the import http://stackoverflow.com/questions/970597/change-auto-increment-starting-number – Alan Hay Aug 30 '16 at 07:56
-
I think jpa will not care for mysql it has its own way to create id?? i am using @generated value annotation – Don Jose Aug 30 '16 at 08:04
-
Well if you posted the relevant info that would be clearer. With MySql I would normally use an auto-increment column managed by the database. – Alan Hay Aug 30 '16 at 08:06
-
Is there a way to make jpa do this?? – Don Jose Aug 30 '16 at 08:08
-
"make jpa do this" ? That is why you have strategy on the `@GeneratedValue` annotation. You selected "AUTO" which leaves it for the JPA provider to do what it wants. Just select "IDENTITY" strategy and you should get MySQL AUTOINCREMENT – Neil Stockton Aug 30 '16 at 08:27
-
Assuming your column actually has AUTO_INCREMENT defined you can, of course, do like this question https://stackoverflow.com/questions/1485668/how-to-set-initial-value-and-auto-increment-in-mysql – Neil Stockton Aug 30 '16 at 08:41
-
Sorry if i am wrong i have many tables so it will be difficult to write auto increment query for all,instead if i have @GeneratedValue(strategy=GenerationType.AUTO & something like init value=1000) my problem solved – Don Jose Aug 30 '16 at 08:46
-
You can't using JPA (without changing your strategy). I already said that AUTO means you leave it to the implementation, so you've thrown away control at that point. You can only control init value when using SEQUENCE or TABLE – Neil Stockton Aug 30 '16 at 09:00
2 Answers
1
Use the GenerationType.TABLE
approach here in order to feed the data based on what value already present in your database.

Prashant Katara
- 95
- 1
- 1
- 14
-
Is there any other way?? If i change @generated value it will affect whole application – Don Jose Aug 30 '16 at 07:54
-
I don't think any other way left here, try to query OPENJPA_SEQUENCE_TABLE in your database and update the initial value to the one which you want. – Prashant Katara Aug 30 '16 at 14:19
0
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;

Saurabh Oza
- 169
- 4
- 18
-
as long as the database being used supports SEQUENCEs (which MySQL doesn't AFAIK). – Aug 25 '18 at 15:16