• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate oracle sequence produces large gap

 
heng zhang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am using hibernate 3 , oracle 10g. I have a table: subject. The definition is here



when insert a new subject, sub_seq is used to create an subject id, the definition is here



the Subject class is like this:



in the subject table , there have been 4555 subjects in the database loaded by plsql scripts from excel and the sub_sequence worked fine. subject ids range from 1--4555.

however, when i added a subject from my application using hibernate,
the sequence number jumped to 255050. After several days running, the subject ids generated by hibernate look like this

270079
270078
270077
270076
270075
270074
270073
270072
270071
270070
270069
270068
270067
270066
270065
270064
270063
270062
270061
270060
270059
270058
270057
270056
270055
270054
270053
270052
270051
270050
265057

265056
265055
265054
265053
265052
265051
265050
260059
260058
260057
260056
260055
260054
260053
260052
260051
260050
255067

255066
255065
255064
255063
255062
255061
255060
255059
255058
255057
255056
255055
255054
255053
255052
255051
255050
4555

4554
4553
.
.
.
.
1


There are several large gaps: 4555 to 255051, 255067 to 260051, 265057 to 270051

this is a waste and not a desired behavior.

does anyone know why this happens and hot to fix it

Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how sequences are designed to work. Why is is a waste and why does it matter?
 
heng zhang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, the sequence purpose is to preserve uniqueness, not a set of sequence number. gaps are allowed.
however, in this case, javax.persistence.SequenceGenerator is wrongly mapped to "seqhilo" and not "sequence" by org.hibernate.cfg.AnnotationBinder
a sequence hilo generator, with a default allocation size of 50. This means that if the sequence value is 5000, the next generated value will be 5000 * 50 = 250000. Add the cache value of the sequence to the equation, and it might explain the huge initial gap.

there is a bug report here
https://issues.jboss.org/browse/EJBTHREE-597


there are two ways to fix this problem
1#
In the SequenceGenerator annotation, add allocationSize = 1, initialValue= 1
2#
instead of using javax.persistence.SequenceGenerator, use org.hibernate.annotations, like this:

@javax.persistence.SequenceGenerator(name = "Question_id_sequence", sequenceName = "S_QUESTION")

@org.hibernate.annotations.GenericGenerator(name="Question_id_sequence", strategy = "sequence", parameters = { @Parameter(name="sequence", value="S_QUESTION") } )

i have tested both ways, which works just fine.

Cheers
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic