Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Problems using a database sequence in JPA

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic