Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in generating the Primary Key using a Sequence

 
Amit Chauhan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,
I am trying to persist an object and also generating the primary key using a default sequence . But the problem is that the Primary key is getting incremented by 2 instead of 1 . My code looks like this :
----------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------

Please provide me with some pointer as to know where the things are going wrong.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your class mapping look like. When you say the default sequence, do you mean something like "hibernate_seq" and then therefore more than one object is mapped to generating the PK from that sequence table, such that I insert one record in table A it gets id 1, then insert one record into table B it gets id 2, then insert again into table A and it gets id 3?

Or do you have it mapped for objects A mapped to Table A and Sequence "a_seq" and for Object B mapped to Table B and sequence "b_seq"

Mark
 
Erik Bengtson
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A sequence in JPA maps to a database sequence. You should check your DB first.

select <seqname>.NEXTVAL FROM DUAL

if it gets incremented by 1, then check your code, later report a bug to the jpa vendor
 
Amit Chauhan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
By default sequence I mean that I have not created it using sql at the database rather I am creating it directly while code .

Here is the entity bean code :



*******The most strange thing is that I find the value of sequence being incremented by 2 and not by 1 .


Kindly help me out .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic