问题描述
我需要为我的数据库主键列生成唯一的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.
推荐答案
看看
它有一个LongGenerator,它可以为你提供你所需要的。
Take a look at http://commons.apache.org/sandbox/commons-id//index.htmlIt has a LongGenerator that can give you exactly what you need.
另外如果你使用的是Hibernate,那么你可以要求它为你生成ID(它有您可以选择几种算法),如果不是,您可以只看一下它们的实现,例如)
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!