These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Generator Increment Problem
PostPosted: Tue Aug 28, 2007 2:15 pm 
Newbie

Joined: Tue Aug 28, 2007 2:09 pm
Posts: 9
Hi,

I am quite new to Hibernate.
I have set the PK of database as Generator Increment type.
Suppose i have 5 rows and i add one row then the ID is set to 6 which is correct.
Now i delete 6th row so there are only 5 rows in the table with ID values 1 to 5 and i commit this change.

Now again when i try to add ID value it automatically sets it to 7 although there are 5 rows in table and 6 th row is deleted. I tried select max(ID) from Table. which gives me 5 as output,
Then from where hibernate finds out that 6 th row was added.
Or is it suppose to work this way?

Thanks.


Top
 Profile  
 
 Post subject: Re: Generator Increment Problem
PostPosted: Tue Aug 28, 2007 2:51 pm 
Beginner
Beginner

Joined: Mon Aug 27, 2007 8:10 am
Posts: 37
c_java wrote:
Hi,

I am quite new to Hibernate.
I have set the PK of database as Generator Increment type.
Suppose i have 5 rows and i add one row then the ID is set to 6 which is correct.
Now i delete 6th row so there are only 5 rows in the table with ID values 1 to 5 and i commit this change.

Now again when i try to add ID value it automatically sets it to 7 although there are 5 rows in table and 6 th row is deleted. I tried select max(ID) from Table. which gives me 5 as output,
Then from where hibernate finds out that 6 th row was added.
Or is it suppose to work this way?

Thanks.


This is the way it works. Is there any reason for you to keep your IDs continuous?


Top
 Profile  
 
 Post subject: Re: Generator Increment Problem
PostPosted: Tue Aug 28, 2007 3:26 pm 
Newbie

Joined: Tue Aug 28, 2007 2:09 pm
Posts: 9
Yes, I mean i don't want to unnecessarily block primary key (ID) for the records which don't exist. which were added but deleted. I want to set the Increment ID to the max(ID) + 1 .( for e.g there are 10 records then 11 and not 21 assuming that there were 20 records once upon a time)

How to achive this?


Top
 Profile  
 
 Post subject: Re: Generator Increment Problem
PostPosted: Tue Aug 28, 2007 3:40 pm 
Beginner
Beginner

Joined: Mon Aug 27, 2007 8:10 am
Posts: 37
c_java wrote:
Yes, I mean i don't want to unnecessarily block primary key (ID) for the records which don't exist. which were added but deleted. I want to set the Increment ID to the max(ID) + 1 .( for e.g there are 10 records then 11 and not 21 assuming that there were 20 records once upon a time)

How to achive this?


You can write your own sequnce generator which does
Code:
select coalesce(max(ID),0)+1 from mytable

Howewer, I would not recommend chasing this goal. You may run into undesired lock on your table.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.