I am using Hibernate (JPA) with Derby database – my app server is Jboss 6.0.
I have to pre-populate a table with 50000 employee info and for that I have used a standalone JDBC program.
Now from my application I also need to insert new employee info on fly, in the same table and at that time I use JPA (since it runs within app container).
My Employee class is an Entity bean and has “Id” column being primary key with “@GeneratedValue(strategy = auto)” annotation so that Hibernate figures out the appropriate running serial no for the primary key (i.e. Id column).
Now the problem is Hibernate always starts this running serial from “1” even if the Employee table already has entries. Thus from application when I try to insert a new record using Entitymanager::persist(), I always get constrain violation error like “duplicate key value in a unique or primary key constraint or unique index“.
This is because hibernate tries to insert the new entry with Id =1 where as I already have an entry in that table with Id = 1.
My question is
a) how can I update Hibernate’e cache so that my running serial number for “Id” column can be accordingly adjusted?
I understand it will vary from DB to DB – but is it possible at all?
b) Is there any other way to achieve the same?
c) If I write a standalone JPA program to do the bulk insert (i.e. pre population) and then start my Jboss application, will hibernate be able to determine the right “running serial” no?
Thanks in advance for any suggestion,
– kuntal
You can generate primary-key using @TableGenerator which generates the next id according to the value in a table maintained having two fields tableName & its respective id & it updates it accordingly after insertion.
It is responsible for maintaining the correct sequence, you can modify initial value etc.
So at the end, create two generators for – 1) application & 2) standalone program, consuming common table for id generation, so there will be no duplicate key value in a unique or primary key constraint or unique index.