I have a database with existing data, where previously I’ve been using AUTO_INCREMENT on the db to generate the PK.
To improve performance and facilitate bulk inserts, I’m trying to introduce a different key generation strategy:
public class BaseEntity implements Serializable {
@Id
@GeneratedValue(generator="generator")
@GenericGenerator(
name="generator", strategy="hilo")
private Integer id;
I’ve found documentation on the @GenericGenerator syntax hard to come by, so I’m not sure if I’ve implemented it correctly. As I understand it, this generator generate a PK for the entity PRIOR to performing the INSERT, therefore preventing the need for a post-insert query to discover the pk.
However, it’s resulting in a org.hibernate.NonUniqueObjectException exception when trying to do an INSERT – presumably because the values hibernate is attempting to use for the PK’s are already taken.
Is my understanding here correct, and – if so – how do I configure Hibernate to read the appropriate tables on startup and select the lower PK bounds?
It seems you are using
tablehilo. Anyways, theGeneratorcode is very important to troubleshoot the thing. So, please provide the relevant snippet.Furthermore, I strongly suggest you to have a look at this approach here, if you have not already had.