java uuid to long,如何使用UUID生成唯一的Long

java uuid to long,如何使用UUID生成唯一的LongIhavearequirementtogenerateuniqueLongidsformydatabaseprimarykeycolumn.IthoughticanuseUUID.randomUUID().getMostSignificantBits()butsometimesitsgeneratingsomenegativelongalso…

java uuid to long,如何使用UUID生成唯一的Long

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

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

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.

解决方案

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.

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)

今天的文章java uuid to long,如何使用UUID生成唯一的Long分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/26403.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注