如何使用UUID生成唯一的Long [英] How to generate unique Long using UUID

查看:2558
本文介绍了如何使用UUID生成唯一的Long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我的数据库主键列生成唯一的Long ID。

I have a requirement to generate unique Long ids for my database primary key column.

我以为我可以使用 UUID.randomUUID()。getMostSignificantBits( )但有时它会产生一些负长也对我来说是个问题。

I thought i can use UUID.randomUUID().getMostSignificantBits() but sometimes its generating some negative long also which is problem for me.

是否有可能只从UUID生成正长?会有数十亿条目所以我希望每个生成的密钥必须是唯一的。

Is it possible to generate only positive long from UUID ?There will be like billions of entries so i want that each generated key must be unique.

推荐答案

看看 http://commons.apache.org/sandbox/commons-id//index.html
它有一个LongGenerator,它可以为你提供你所需要的。

Take a look at http://commons.apache.org/sandbox/commons-id//index.html It has a LongGenerator that can give you exactly what you need.

另外如果你使用的是Hibernate,那么你可以要求它为你生成ID(它有您可以选择几种算法),如果不是,您可以只看一下它们的实现,例如 http://grepcode.com/file/repo1.maven.org/maven2/hibernate/hibernate/2.1。 8 / net / sf / hibernate / id / TableHiLoGenerator.java#TableHiLoGenerator

In addition if you are using Hibernate then you can ask it to generate IDs for you (it has several algorithms you can choose from), in if not you can just take a look at their implementation for example http://grepcode.com/file/repo1.maven.org/maven2/hibernate/hibernate/2.1.8/net/sf/hibernate/id/TableHiLoGenerator.java#TableHiLoGenerator)

这篇关于如何使用UUID生成唯一的Long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆