aspose file tools*
The moose likes Object Relational Mapping and the fly likes  Problem in generating the Primary Key  using a  Sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark " Problem in generating the Primary Key  using a  Sequence" Watch " Problem in generating the Primary Key  using a  Sequence" New topic
Author

Problem in generating the Primary Key using a Sequence

Amit Chauhan
Greenhorn

Joined: Jun 26, 2007
Posts: 27
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Erik Bengtson
Ranch Hand

Joined: Dec 06, 2003
Posts: 90
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

Joined: Jun 26, 2007
Posts: 27
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 .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem in generating the Primary Key using a Sequence