-->
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.  [ 8 posts ] 
Author Message
 Post subject: how to get the next SQL SERIAL type value?
PostPosted: Mon May 24, 2004 5:11 pm 
Beginner
Beginner

Joined: Thu Apr 29, 2004 4:03 pm
Posts: 40
Hi,

I have a column in my table with serial type. the problem is, that I need an id for my object and operate with this id BEFORE the object is saved. However, the hibernate assigns an id AFTER the object is saved.

so I have sommething like this:
TABLE mytable (
userid SERIAL )

and my object
class User {
Integer userID;
...
}

Somewhere I read that I can get the next value by SELECT nextval('tablename_serialcolumn_seq'), but it didn't work, my database (informix) couldn't recognize this command.

Any ideas how to get a value for my Integer property and stay consistent with serial column?

(please comment wheter you solution is thread-safe (the racing conditions are ok)

thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 4:40 am 
Beginner
Beginner

Joined: Thu Apr 29, 2004 4:03 pm
Posts: 40
anyone any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 29, 2004 11:15 pm 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
Why do you want the id before you save? If it is because you want to put the id in another table/object, you don't need the id, as you can work with Hibernate to do the updating in the right order.

Sherman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 03, 2004 9:56 am 
Beginner
Beginner

Joined: Thu May 13, 2004 5:51 am
Posts: 28
Try this:

select Sequencename.nextval from systables where tabid=1


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 05, 2004 6:04 pm 
Beginner
Beginner

Joined: Thu Apr 29, 2004 4:03 pm
Posts: 40
sgwood wrote:
Why do you want the id before you save? If it is because you want to put the id in another table/object, you don't need the id, as you can work with Hibernate to do the updating in the right order.

Sherman


yes, I need it for that reason. I hava an object with ID1, and it contains another object, which ID is composed of parent object's ID1 and another number.


the problem is, that I didn't map that complex object in hibernate, but I mapped the plain properties of parent object, and then i mapped the properties of the child object. and if I want to save the parent object, I simply manually save the child, and than the parent

Should I add mapping of child object to the parent object? I'm not sure, It will work....


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 06, 2004 7:52 pm 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
You are making life very difficult for yourself.

Have your child and parent have separate generated ids in your Hibernate mapping.

Then all you need to do is:

child.setParent(parent);
session.saveOrUpdate(parent);
session.saveOrUpdate(child);

This is not a legacy database you are using Hibernate with, is it?

Sherman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 10, 2004 6:42 pm 
Beginner
Beginner

Joined: Thu Apr 29, 2004 4:03 pm
Posts: 40
sgwood wrote:
You are making life very difficult for yourself.

Have your child and parent have separate generated ids in your Hibernate mapping.

Then all you need to do is:

child.setParent(parent);
session.saveOrUpdate(parent);
session.saveOrUpdate(child);

This is not a legacy database you are using Hibernate with, is it?

Sherman



could you give me your email?
yes it's a legacy database, we have an old database schema to which we have to adopt our objects


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 11, 2004 1:08 am 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
You need to look into using composite-id. Check out the documentation and the forums to see how this is used.


Sherman


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