Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ulid-java

CI Maven Central javadoc

Generate and parse ULIDs in Crockford base32 text and binary representations.

See ULID specification for more info

Humand fork

This is the Humand fork of jaspeen/ulid-java, published to Humand CodeArtifact as co.humand.io.github.jaspeen:ulid-java.

Fork changes:

  • ULID.randomV7() generates UUIDv7-compatible ULIDs
  • 0.3.0-hu+ requires Hibernate ORM 7 (new IdentifierGenerator APIs); Hibernate 6 users should stay on 0.2.1-hu

Key points

  • Java 17+
  • API similar to java.util.UUID
  • Optional monotonic generator
  • Optional hibernate type and ID generator (requires hibernate 7.x)

Install

Maven

<dependency>
    <groupId>co.humand.io.github.jaspeen</groupId>
    <artifactId>ulid-java</artifactId>
    <version>0.3.0-hu</version>
</dependency>

Usage

ULID generation

ULID ulid = ULID.randomV7();
String crockfordBase32 = ulid.toString();
byte[] binary = ulid.toBytes();

Parsing

ULID parsedFromString = ULID.fromString("3ZFXZQYZVZFXZQYZVZFXZQYZVZ");
ULID parsedFromBytes = ULID.fromBytes(
        new byte[] {127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127});
assertEquals(parsedFromString, parsedFromBytes);

UUID compatibility

ULID.randomV7().toUUID();
ULID.fromUUID(UUID.randomUUID());

Monotonic ULID generation

MonotonicULID.random();

Hibernate ID generator

Hibernate is not added as transitive dependency, it should be specified additionally

@Entity
class ULIDEntity {
    @Id
    @GeneratedValue(generator = "ulid")
    @GenericGenerator(name = "ulid", strategy = "io.github.jaspeen.ulid.hibernate.ULIDIdGenerator")
    private ULID id;
}
 
 // This will generate UUID using ULID algorithm providing ordered keys 
 // while keeping other stuff same
@Entity 
class UUIDEntity {
    @Id
    @GeneratedValue(generator = "ulid")
    @GenericGenerator(name = "ulid", strategy = "io.github.jaspeen.ulid.hibernate.ULIDIdGenerator")
    private UUID id;
}

Generator can be defined in package-info.java for all entities instead of field annotation in every entity

@GenericGenerator(name = "ulid", strategy = "io.github.jaspeen.ulid.hibernate.ULIDIdGenerator")
package my.service.model;

import org.hibernate.annotations.GenericGenerator;

Notes

For java 8 and hibernate 5 use verions 0.1.x

About

Java ULID implementation

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages