Java

Generate UUID in Java

Use the built-in java.util.UUID class — available since Java 1.5, no dependencies needed. Includes Spring Boot JPA and REST patterns.

Need a UUID right now? Generate one instantly in your browser.

Open UUID v4 Generator →

The Quick Answer

Java's standard library includes java.util.UUID since Java 1.5. No Maven or Gradle dependency needed.

Java — generate UUID v4
import java.util.UUID;

UUID id = UUID.randomUUID();
System.out.println(id.toString());
// → "f47ac10b-58cc-4372-a567-0e02b2c3d479"

// As a plain string
String idStr = UUID.randomUUID().toString();

// Without hyphens
String compact = idStr.replace("-", "");

Spring Boot — JPA Entity with UUID Primary Key

Spring Boot 3 / Hibernate 6 — UUID primary key
import jakarta.persistence.*;
import java.util.UUID;

@Entity
@Table(name = "products")
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)  // Hibernate 6+ / Spring Boot 3+
    private UUID id;

    @Column(nullable = false)
    private String name;

    // Getters / setters ...
}
Spring Boot 2 / Hibernate 5 — older approach
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.UUID;

@Entity
public class Product {

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(columnDefinition = "BINARY(16)")
    private UUID id;
}

Spring Boot — REST Controller with UUID Path Variable

Spring Boot — REST controller
import org.springframework.web.bind.annotation.*;
import java.util.UUID;

@RestController
@RequestMapping("/api/products")
public class ProductController {

    @GetMapping("/{id}")
    public Product getProduct(@PathVariable UUID id) {
        // Spring automatically parses and validates the UUID path variable
        return productService.findById(id);
    }

    @PostMapping
    public Product createProduct(@RequestBody CreateProductRequest req) {
        // UUID generated by JPA @GeneratedValue
        return productService.create(req);
    }
}

Name-Based UUIDs (v3 and v5)

Java — UUID v3 (name-based, deterministic)
import java.util.UUID;

// UUID v3 — MD5 hash of a name within a namespace
// Same inputs always produce the same UUID
UUID nameId = UUID.nameUUIDFromBytes("example.com".getBytes());
System.out.println(nameId);
// → "9073926b-929f-31c2-abc9-fad77ae3e8eb"

// Note: Java stdlib only has v3 (MD5). For v5 (SHA-1), use a library.

Frequently Asked Questions

Do I need a dependency to generate UUIDs in Java?

No. java.util.UUID has been part of the Java standard library since Java 1.5. Just call UUID.randomUUID() — no Maven or Gradle dependency needed.

Is UUID.randomUUID() thread-safe in Java?

Yes. UUID.randomUUID() uses SecureRandom internally, which is thread-safe. You can call it from multiple threads concurrently without synchronization.

How do I use a UUID as a primary key in Spring Boot JPA?

Annotate the ID field with @Id and @GeneratedValue(strategy = GenerationType.UUID) (Spring Boot 3+ / Hibernate 6+), or use @GeneratedValue(generator = "uuid2") with the Hibernate UUIDGenerator for older versions. PostgreSQL, MySQL 8+, and H2 all support UUID column types natively.

Should I store UUIDs as VARCHAR or BINARY in MySQL?

Store as BINARY(16) for best performance. A BINARY(16) column uses 16 bytes versus 36 bytes for VARCHAR(36), and comparisons are faster. Use UUID_TO_BIN(uuid, 1) (the second argument reorders bytes for better index locality) and BIN_TO_UUID(bin, 1) to convert. In Hibernate, annotate with @Column(columnDefinition = "BINARY(16)").

Related Tools & Guides