Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in generating a sequence for the Primary Key

 
Amit Chauhan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"AndyJames Rocks"

Welcome to JavaRanch!

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

Thanks!
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amit Chauhan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic