Forums Register Login

Problem in generating a sequence for the Primary Key

+Pie Number of slices to send: Send
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 .
+Pie Number of slices to send: Send
"AndyJames Rocks"

Welcome to JavaRanch!

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

Thanks!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 917 times.
Similar Threads
Hibernate-Oracle Sequence...
How to represent a sequence in oracle in HQL
need help with design of a Login page in JSP
Funny number --- oracle sequence + Hibernate
Referential Integrity: How 2 INSERT the professional way ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:03:43.