-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to control auto-generated running serial no for a Table?
PostPosted: Tue Jan 11, 2011 12:47 am 
Newbie

Joined: Tue Jan 11, 2011 12:35 am
Posts: 5
Hi All, I am using Hibernate (JPA) with Derby database - my app server is Jboss 6.0. I have to pre-populate a table with 50000 employee info and for that I have written a standalone JDBC program. Now from my Jboss-application I also need to insert new employee info on fly, in the same table and at that time I use JPA (since it runs within app container).

My Employee class is the Entity bean and has "Id" column being primary key with "@GeneratedValue(strategy = auto)" annotation so that Hibernate figures out the appropriate running serial no for the primary key (i.e. Id column).

Now the problem is Hibernate always starts this running serial number from "1" even if the Employee table already has entries (since I populated it thr the JDBC program). Thus from my jboss-application when I try to insert a new record using Entitymanager::persist(), I always get constrain violation error like "duplicate key value in a unique or primary key constraint or unique index". This is because hibernate tries to insert the new entry with Id =1 where as I already have an entry in that table with Id = 1.

My question is

a) How can I instruct Hibernate so that it starts generating "running serial no" based on total count of entries exist in that specific table? For example if the Employee table already contains 100 rows, I would expect Hibernate to start the auto-generation of the running serial from 101 onwards.
Can I do this in any way? like thru hibernate property/configuration change or implementing a special class, etc.?

b) Is there any other way to achieve the same?

c) If I write a standalone JPA program to do the bulk insert (i.e. pre-population) and then start my Jboss application, will hibernate be able to determine the right "running serial" no?

Thanks in advance for any suggestion, - kuntal


Top
 Profile  
 
 Post subject: Re: How to control auto-generated running serial no for a Table?
PostPosted: Fri Jan 14, 2011 1:02 pm 
Newbie

Joined: Fri Jan 14, 2011 12:25 pm
Posts: 2
In @PrePersist method you can query the table and get the next value of your Id and set it in the object, this way you wont need the sequence generator.


Top
 Profile  
 
 Post subject: Re: How to control auto-generated running serial no for a Table?
PostPosted: Fri Jan 14, 2011 2:42 pm 
Newbie

Joined: Tue Jan 11, 2011 12:35 am
Posts: 5
gmanoj77 wrote:
In @PrePersist method you can query the table and get the next value of your Id and set it in the object, this way you wont need the sequence generator.

Hi gmanoj77,
Can you pls. be more specific and provide a code snippet for this? I have the "Id" column, being the primary key, in my Employee entity bean with @GeneratedValue(strategy = auto)" annotation. Thus I do not (and probably can not) need to set the Id value while inserting a new record - hibernate does that for me.
Now if I have a @PrePersist method, how can I set this Id value?
Also do you mean I have to do this setting for every new insert? - then it will be a overkill.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.