Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems using a database sequence in JPA

 
Ela Jarecka
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a recurring problem while trying to use an existing sequence from our database. I annotated the field in the entity bean in the following way:

First try:
@Id
@GeneratedValue( strategy=GenerationType.SEQUENCE, generator="seq_wiedervorlage" )
@Basic( optional = false )
@Column( name = "ID" )
private Integer id;

Second try ( read in the upcoming Pro JPA 2.0 book )
@Id
@SequenceGenerator( name="gen_resubmission", sequenceName="seq_resubmission" )
@GeneratedValue( strategy=GenerationType.SEQUENCE, generator="gen_resubmission" )
@Basic( optional = false )
@Column( name = "ID" )
private Integer id;

hoping that while I persist a new resubmission in the database the ( existing ) sequence called "seq_resubmission" would be used. It seems to work, but unfortunately
not all the time. Sometimes I manage to add several resubmissions in a row but then when I restart my web application it tries to insert a new record with an "old"
id again and I get the "ORA-00001: unique constraint (DID.PKEY_RESUBMISSION ) violated" exception!!! Strangely enough, it always seems to revert to the same previously
used id, no matter how many resubmissions I have added in the meantime.

Does anyone have an idea what I am doing wrong? Thanks in advance for any help.
 
Sujeet sharma
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even I am also facing same problem....any pointer?
 
Sujeet sharma
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujeet sharma wrote:Even I am also facing same problem....any pointer?


I find the solution. Just define allocation size in hibernate sequence. allocation size=1 solved my problem. you can increase it if you are doing bulk insert.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic