JBoss EAP 6.x with Hibernate Oracle Sequence Duplicate Value on Primary Key -


i have defined number of hibernate entities using pure jpa annotations. these use predefined oracle sequence on database automatically generate primary key values.

@id @sequencegenerator(name = "users_id_generator", sequencename = "my_seq") @generatedvalue(strategy = generationtype.sequence, generator = "users_id_generator") @column(name = "u_id", updatable = false, unique = true, nullable = false, precision = 19) private long id; 

when deployed jboss eap 6.1 works after short period hibernate starts generating duplicate keys on inserts (ora-00001 errors).

i don't care id ordering or gaps, can't tolerate duplicate keys... going on here?

this not documented, many of solutions on here , other sites relate older versions of hibernate hilo sequencegenerator default. after investigation found underlying cause jboss eap 6 sets

hibernate.id.new_generator_mappings=true  

by default, uses org.org.hibernate.id.enhanced.sequencestylegenerator instead of older version.

the hibernate sequencestylegenerator default increment 1 (check code!), jpa overrides increment value in generator 50. means generator looks @ sequence nextval , keeps cache of 50 ids use, starting nextval - 49. when these exhausted generator reads next sequence oracle, , repeats process. once first series of ids exhausted start seeing duplicate keys.

so resolution is:

1) either define oracle sequence(s) increment value of 50 match jpa default

create sequence my_seq start 50 maxvalue 9999999999999999999 increment 50 nocycle; 

or

2) add allocationsize=1 @sequencegenerator annotation - forces sequencegenerator go read next value oracle sequence each id requires (with potential performance impact)

 @sequencegenerator(name = "users_id_generator", sequencename = "my_seq", allocationsize = 1) 

, or

3) define oracle sequence increment other value, , ensure allocationsize matches.

answered own question in hope of helping others strike issue.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -