File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Problem in generating a sequence for the Primary Key Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Problem in generating a sequence for the Primary Key" Watch "Problem in generating a sequence for the Primary Key" New topic
Author

Problem in generating a sequence for the Primary Key

Amit Chauhan
Greenhorn

Joined: Jun 26, 2007
Posts: 27
Hi All ,
I have a simple POJO where I want to generate the Primary key using a sequence that increments value by 1 each time a new row gets inserted , .Here is the sample code:


public class Tab1 implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "tabGen")
@SequenceGenerator(name="tabGen" , allocationSize=1)
@Column(name="ID")
private long id;


But the problem with this is that it increments the value by 2 and not by 1 . Please can anybody help me out .

* I am using orcale 9i as the database .
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"AndyJames Rocks"

Welcome to JavaRanch!

Please update your display name so it complies with our Naming Policy. You can do this here.

Thanks!


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
That's odd. Are you generating the schema so that the provider is creating the sequence or using a pre-existing sequence? If generated, can you see the DDL that is generated? It should have an INCRMENT BY 1 clause.

Is it possible that you are allocating an entity and then rolling it back so that the next id you see is incremented by two? It's possible that the sequence update is occurring in a separate transaction so even though you roll back persisting the entity the sequence allocation is not rolled back.

Just guessing.


-Mike
Pro JPA 2: Mastering the Java Persistence API
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16622

Now that we have your duplicate threads closed, we will keep this one open for replies.

Amit, what type of application are you writing? The reason why I am asking is because of the same reason why someone had the same issue with having a call to nextVal() in a JSP page. It could be a lifecycle issue with your "framework" that is sending the object twice, instead of once. So where this is being called is in the middle of a lifecycle that is getting called twice.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Amit Chauhan
Greenhorn

Joined: Jun 26, 2007
Posts: 27
Hi ,
What I am trying to do is , simply trying to persist a new Object every time the main method gets called and could not see anypoint where there are multiple / double call to sequence. The sequence that I am using is not already defined in the database , I have defined it in the code itself.
Here is the code :


[ June 27, 2007: Message edited by: Mark Spritzler ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16622

Hmm, very interesting, there isn't a reason, based on your code, that it would fire off the sequence twice.

by any chance can you post your CREATE SEQUENCE code?

Also, you can always edit your own posts, just like I did to format your code, there is an icon above your post, that is like a blank page, and it will take you to an edit screen to edit your post. This is also how you delete a duplicate thread that you create. If you edit the top post of the thread, there is a Delete checkbox to click and then click the Post button (of course, you had to be the one to create it in the first place)

Also, there are buttons under the Add Reply button, one of them is labelled "CODE" use this to create CODE tags so that the code you post can keep its indentation, which makes it easier to read.

Thanks

Mark
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Your discriminator column should not be mapped to your id column. Not sure what your SQL looks like, but it is not a good idea to have two writable mappings to the same column. Many providers will even throw a validation exception if this is detected.
 
 
subject: Problem in generating a sequence for the Primary Key
 
Threads others viewed
Referential Integrity: How 2 INSERT the professional way ?
Hibernate-Oracle Sequence...
How to represent a sequence in oracle in HQL
Funny number --- oracle sequence + Hibernate
need help with design of a Login page in JSP
IntelliJ Java IDE