Java has included java.util.UUID since Java 1.5, making UUID v4 generation straightforward. UUID v7 support is newer — there is no UUID.timeBasedUUID() in the standard library yet, but the ecosystem has adapted quickly with Hibernate 6.5+, the JUG library, and upcoming JEP proposals.

Generating UUIDs in Java

UUID v4 (random) — standard library:

import java.util.UUID;

UUID id = UUID.randomUUID(); // v4
String str = id.toString();  // "550e8400-e29b-41d4-a716-446655440000"

UUID v7 — using java-uuid-generator (JUG):

<!-- pom.xml -->
<dependency>
  <groupId>com.fasterxml.uuid</groupId>
  <artifactId>java-uuid-generator</artifactId>
  <version>5.0.0</version>
</dependency>
import com.fasterxml.uuid.Generators;

UUID id = Generators.timeBasedEpochGenerator().generate(); // v7

JUG implements the RFC 9562 monotonic counter within each millisecond window, so rapid successive calls remain ordered even when the system clock has not advanced.

Spring Data JPA and UUID Primary Keys

Spring Boot auto-configures Hibernate, so JPA entity IDs map directly to database uuid columns (PostgreSQL) or BINARY(16) (MySQL) when using the correct annotations.

UUID v4 with @GeneratedValue:

@Entity
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;
}

GenerationType.UUID was added in Jakarta Persistence 3.1 (Spring Boot 3.1+). It delegates to Hibernate’s UUID generator, which defaults to v4.

UUID v7 with Hibernate 6.5+:

@Entity
public class Order {
    @Id
    @GeneratedValue
    @UuidGenerator(style = UuidGenerator.Style.TIME)
    private UUID id;
}

Style.TIME in Hibernate 6.5+ produces RFC 9562 v7 values. Earlier versions of Hibernate produced v1-style values — check your Hibernate version before relying on this annotation for v7.

Performance Considerations

The same B-tree index argument applies in Java as in other stacks: UUID v4 primary keys generate random inserts that fragment the clustered index over time. UUID v7 keeps inserts near the rightmost leaf page.

For PostgreSQL backends with Spring Data JPA, the difference is measurable at around 5–10 million rows. Switching to @UuidGenerator(style = UuidGenerator.Style.TIME) is a one-line change and requires no schema migration — the column type stays uuid.

MySQL with BINARY(16)

For MySQL backends, configure the uuid property mapping to BINARY(16):

@Entity
public class Order {
    @Id
    @Column(columnDefinition = "BINARY(16)")
    @GeneratedValue
    @UuidGenerator(style = UuidGenerator.Style.TIME)
    private UUID id;
}

Hibernate handles the UUID ↔ BINARY(16) conversion transparently. Queries using the ID will use the binary index efficiently.

Frequently asked questions

How do I use UUID v7 as a primary key in Java with Hibernate?

Hibernate 6.5+ supports UUID v7 via @UuidGenerator(style = UuidGenerator.Style.TIME). For older Hibernate versions, use the java-uuid-generator (JUG) library: Generators.timeBasedEpochGenerator().generate(). No schema migration is needed — the database uuid column type stores both v4 and v7 identically.

Do UUIDs hurt database index performance?

UUID v4 does — its randomness causes every insert to land at a different leaf page, leading to page splits and poor cache locality. UUID v7 embeds a millisecond timestamp so consecutive inserts cluster together, behaving like an auto-increment integer for B-tree purposes.

Does PostgreSQL 18 support UUID v7 natively?

Yes. PostgreSQL 18 ships a built-in uuidv7() function that generates RFC 9562-compliant UUID v7 values directly in SQL — no extensions or application-level generation required. The values store in the existing uuid column type unchanged.

How do I generate a UUID in Java?

Java's standard library includes java.util.UUID. Use UUID.randomUUID() for UUID v4. For UUID v7, the standard library does not yet include a native method — use a library such as java-uuid-generator (JUG): Generators.timeBasedEpochGenerator().generate() returns a v7 UUID.

Does Hibernate support UUID v7 primary keys?

Hibernate 6.2+ introduced the @UuidGenerator(style = UuidGenerator.Style.TIME) annotation, which generates time-based UUIDs. In Hibernate 6.5+ this maps to RFC 9562 UUID v7 values. For earlier versions, implement a custom IdentifierGenerator using a library like JUG. See the Hibernate User Guide for configuration details.