Generate domain object identifiers from UUIDs that are globally unique and sortable in their binary and textual representation.
-
Add the following dependency to your pom.xml:
<dependency> <groupId>com.buralo</groupId> <artifactId>buralo-identifier-core</artifactId> <version>1.2.0</version> </dependency>
-
Instantiate
UUIDIdentifierService
which implements theIdenterifierService
:private final IdentifierService identifierService = new UUIDIdentifierService();
-
Add the following dependencies to your pom.xml:
<dependency> <groupId>com.buralo</groupId> <artifactId>buralo-identifier-core</artifactId> <version>1.2.0</version> </dependency>
<dependency> <groupId>com.buralo</groupId> <artifactId>buralo-identifier-spring</artifactId> <version>1.2.0</version> </dependency>
-
The
buralo-identifier-spring
contains auto-configuration that exports anIdenifierService
andIdentifierConverter
bean. TheIdentifierConverter
bean allows conversion from the string r -
Auto-wire the
IdentifierService
bean:@Autowired private IdentifierService identifierService;
-
Generate an identifier:
var id = identifierService.generate();
-
id
is a record of typeUUIDIdentifier
which implements theIdentifier
interface. -
id.binary()
returns abyte[16]
arrary that con be stored in aBINARY(16)
column in the database.PreparedStatement ps = /* ... */; ps.setBytes(1, id.binary())
-
id.text()
returns a 22 character URL-safe string that can be used in RESTful APIs as part of the URI path or in request/response payloads.
-
If you read
byte[16]
from aBINARY(16)
database column use theidentifierService.fromBinary(bytes [])
method to convert it to anIdentifier
:ResultSet rs = /* ... */; var bytes = rs.getBytes(1); var id = identifierService.fromBinary(bytes);
-
To convert a
String
passed via request URI or payload you can use theidentifierService.fromText(String)
method.
-
Type 1 UUIDs contain the timestamp of the generation time. This means the identifier can do double duty since it is both the unique identifier and the creation date/time.
-
You can extract the timestamp from the identifier using
IdentifierService.toInstant(Identifier)
. -
If you want to search for entities created with a certain time window you can us
IdentifierService.asLowerBound(Temporal)
andIdentifierService.asUpperBound(Temporal)
to get identifiers to use in the range query.
The Buralo Identifier is made available under the Apache License and the source code is hosted on GitHub at https://github.com/BuraloOSS/buralo-identiifer.