简体   繁体   English

Java全局标识符

[英]Java Global Identifiers

I am using Java EE and the Spring framework. 我正在使用Java EE和Spring框架。 I am writing my POJOs, and the base object for my system should have a globally unique ID. 我正在编写我的POJO,并且系统的基础对象应该具有全局唯一ID。 My question is: what would be a "best-practice" way for me to generate this ID. 我的问题是:什么是我生成此ID的“最佳实践”方式? This id must also be stored in a db table as a primary key. 此ID还必须作为主键存储在db表中。

UUID类在生成通用唯一标识符方面做得很好: 在Java API中

If you're using it as a primary key in the database, it may be better to use a sequence from within the database. 如果将其用作数据库中的主键,则最好使用数据库中的序列。 Depends heavily on what you're doing, but in general I find it's better to do it this way since you can then leverage database constraints and such. 很大程度上取决于您在做什么,但是总的来说,我发现这样做更好,因为这样您就可以利用数据库约束等。 (IE - let the db take care of making sure the PK is unique, not null, etc.) (即-让数据库负责确保PK是唯一的,而不是null等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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