开发者

NonUniqueObjectException migrating to hilo generator strategy

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 appr开发者_如何转开发opriate tables on startup and select the lower PK bounds?


It seems you are using tablehilo. Anyways, the Generator code 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜