Reputation: 564
I have a Spring based application which uses JPA 2.1 for the persistence layer. I use @TableGenerator and @GeneratedValue annotations to handle my primary key id generation.
These are the annotations:
@TableGenerator(name = "user_gen", table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_val", allocationSize = 1)
…
@GeneratedValue(strategy = GenerationType.TABLE, generator = "user_gen")
I use this entity to insert records in my user table. It was all working good. Now I have a new problem. An external application needs to insert records in my user tables. This causes primary key violation at my end.
Is there any option in JPA which will make my id_gen table's gen_val value updated based on the max id of my user table?
[I could not find such a solution in my research]
Other thoughts to fix the issue are also welcome.
Upvotes: 0
Views: 809
Reputation: 24433
You might make it work with custom id generator where you would check for maximum id before each insert, but I wouldn't recommend it. In my opinion, this would be best handled on database level.
How is that application generating id's? They have to get it from somewhere, so why not why not handle id in that step. Best option would be if it can use the same id_gen
mechanism for this. Several ideas:
Upvotes: 1