简体   繁体   中英

UUID random number generation

I am using UUID Random number as a primary key of Object when i save object in Database. So this generated UUID number should be unique. This way i got random number.


final String UUIDUserToken = UUID.randomUUID().toString()  

Can this generated number unique for any generated number?

Give me idea.

It may repeat (with very little possibility) ,

Update:

try{
  //try inserting
}catch(){
  //SQL constraint fails
  // regenerate new UUID
  //check in DB before inserting
  //insert now, if its unique, else regenerate
}

The generated ID is unique world-wide by definition. BTW, why are you generating primary key in java code? Database knows to do this job very well. Just define the field as AUTO INCREMENT .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM