views:

33

answers:

1

I want to get surrogate keys for my user table(s) in MySQL. I'm sure concatinating an incrementing value + a timestamp would get me unique keys across multiple tables but how do I get the incremental value for the class's persistence table before I persist it to the database.

+1  A: 

let hibernate do it for you using one of their key generators. If you must define your own key scheme, you will have to write your own generator.

hvgotcodes
Is there anything wrong with getting the current system time in millis then assigning this as the ID? I want something a bit more unique then an auto-incrementing field (which is what an @Generated tag is giving me at the moment with MySQL). I want keys that will be unique across several tables not just one
Yes, because, depending on the resolution of your database's CURRENT_TIMESTAMP, you could have two objects being inserted at the same exact moment in time. How can you get more unique than an auto-incrementing field, which is guaranteed to be unique already?
matt b
How do you autoincrement accross multiple tables...
@user398371, I don't think its possible with mysql to do that. Oracle supports it -- I think you define a sequence and then tell each table to use that sequence.
hvgotcodes
Thanks hvgotcodes. Maybe I'll have Oracle someday!