New posts for Spring and Hibernate

This commit is contained in:
2026-11-06 01:05:17 +05:30
parent b83cc9988e
commit 0245c00033
57 changed files with 2113 additions and 0 deletions
@@ -0,0 +1,22 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** Bare @GeneratedValue (strategy AUTO) on a Long. */
@Entity
public class AutoLongItem {
@Id
@GeneratedValue
private Long id;
private String name;
public AutoLongItem() {}
public AutoLongItem(String name) { this.name = name; }
public Long getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,22 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** Bare @GeneratedValue (strategy AUTO) on a UUID. */
@Entity
public class AutoUuidItem {
@Id
@GeneratedValue
private UUID id;
private String name;
public AutoUuidItem() {}
public AutoUuidItem(String name) { this.name = name; }
public UUID getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,25 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
/** Child of {@link Parent}. */
@Entity
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_child")
@SequenceGenerator(name = "seq_child", sequenceName = "seq_child", allocationSize = 50)
private Long id;
private String name;
@ManyToOne(fetch = FetchType.LAZY)
private Parent parent;
public Child() {}
public Child(String name) { this.name = name; }
void setParent(Parent p) { this.parent = p; }
public Long getId() { return id; }
}
@@ -0,0 +1,22 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** Version 7 from the hand-written {@link UuidV7Generator}, plugged in through @UuidGenerator(algorithm = ...). */
@Entity
public class HandRolledV7Item {
@Id
@UuidGenerator(algorithm = UuidV7Generator.class)
private UUID id;
private String name;
public HandRolledV7Item() {}
public HandRolledV7Item(String name) { this.name = name; }
public UUID getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,22 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** GenerationType.IDENTITY: the database assigns the id while the INSERT runs. */
@Entity
public class IdentityItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public IdentityItem() {}
public IdentityItem(String name) { this.name = name; }
public Long getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,27 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
/** Parent of a small graph, used to show what hibernate.order_inserts changes. Sequence ids, so JDBC batching is possible. */
@Entity
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_parent")
@SequenceGenerator(name = "seq_parent", sequenceName = "seq_parent", allocationSize = 50)
private Long id;
private String name;
@OneToMany(mappedBy = "parent", cascade = CascadeType.PERSIST)
private List<Child> children = new ArrayList<>();
public Parent() {}
public Parent(String name) { this.name = name; }
public void add(Child c) { c.setParent(this); children.add(c); }
public Long getId() { return id; }
}
@@ -0,0 +1,22 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** SEQUENCE with no @SequenceGenerator at all: Hibernate chooses the sequence name and allocation size. */
@Entity
public class SeqDefaultItem {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String name;
public SeqDefaultItem() {}
public SeqDefaultItem(String name) { this.name = name; }
public Long getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,23 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** SEQUENCE with allocationSize = 1: one sequence round trip per entity. */
@Entity
public class SeqOneItem {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_one_item")
@SequenceGenerator(name = "seq_one_item", sequenceName = "seq_one_item", allocationSize = 1)
private Long id;
private String name;
public SeqOneItem() {}
public SeqOneItem(String name) { this.name = name; }
public Long getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,23 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** SEQUENCE with allocationSize = 50 (the JPA default): one sequence round trip per 50 entities (pooled optimizer). */
@Entity
public class SeqPooledItem {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_pooled_item")
@SequenceGenerator(name = "seq_pooled_item", sequenceName = "seq_pooled_item", allocationSize = 50)
private Long id;
private String name;
public SeqPooledItem() {}
public SeqPooledItem(String name) { this.name = name; }
public Long getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,22 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** GenerationType.TABLE: ids come from a row in a generator table. */
@Entity
public class TableItem {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
private String name;
public TableItem() {}
public TableItem(String name) { this.name = name; }
public Long getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,25 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.annotations.UuidGenerator;
import org.hibernate.type.SqlTypes;
/** Version 7 UUID stored as text, CHAR(36), instead of the dialect's native/binary uuid type. */
@Entity
public class UuidCharItem {
@Id
@UuidGenerator(style = UuidGenerator.Style.VERSION_7)
@JdbcTypeCode(SqlTypes.CHAR)
private UUID id;
private String name;
public UuidCharItem() {}
public UuidCharItem(String name) { this.name = name; }
public UUID getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,14 @@
package com.ankurm.idgen;
import com.github.f4b6a3.uuid.UuidCreator;
import java.util.UUID;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.uuid.UuidValueGenerator;
/** Adapter: lets {@code @UuidGenerator(algorithm = UuidCreatorV7.class)} use the uuid-creator library's version 7 generator. */
public class UuidCreatorV7 implements UuidValueGenerator {
@Override
public UUID generateUuid(SharedSessionContractImplementor session) {
return UuidCreator.getTimeOrderedEpoch();
}
}
@@ -0,0 +1,22 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** Version 7 from the uuid-creator library, plugged in through @UuidGenerator(algorithm = ...). */
@Entity
public class UuidCreatorV7Item {
@Id
@UuidGenerator(algorithm = UuidCreatorV7.class)
private UUID id;
private String name;
public UuidCreatorV7Item() {}
public UuidCreatorV7Item(String name) { this.name = name; }
public UUID getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,22 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** @UuidGenerator(style = TIME): a version 1 (Gregorian time) UUID with Hibernate's own layout. Not version 7. */
@Entity
public class UuidTimeItem {
@Id
@UuidGenerator(style = UuidGenerator.Style.TIME)
private UUID id;
private String name;
public UuidTimeItem() {}
public UuidTimeItem(String name) { this.name = name; }
public UUID getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,22 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** GenerationType.UUID (JPA 3.1): random, version 4. */
@Entity
public class UuidV4Item {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
private String name;
public UuidV4Item() {}
public UuidV4Item(String name) { this.name = name; }
public UUID getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,22 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** @UuidGenerator(style = VERSION_6): RFC 9562 version 6, the time-ordered re-arrangement of version 1. */
@Entity
public class UuidV6Item {
@Id
@UuidGenerator(style = UuidGenerator.Style.VERSION_6)
private UUID id;
private String name;
public UuidV6Item() {}
public UuidV6Item(String name) { this.name = name; }
public UUID getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,42 @@
package com.ankurm.idgen;
import java.security.SecureRandom;
import java.util.UUID;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.uuid.UuidValueGenerator;
/**
* A hand-written RFC 9562 version 7 generator, about twenty lines of logic. It exists to show what the layout is
* (the shipped {@code Style.VERSION_7} does the same job); do not prefer it to the built-in one.
*
* <pre>
* 48 bits unix_ts_ms milliseconds since 1970-01-01T00:00Z, big-endian: this is what makes ids sort by creation time
* 4 bits version = 0111
* 12 bits rand_a used here as a counter, so two ids created in the same millisecond still increase
* 2 bits variant = 10
* 62 bits rand_b random
* </pre>
*/
public class UuidV7Generator implements UuidValueGenerator {
private static final SecureRandom RNG = new SecureRandom();
private long lastMs = -1;
private int counter;
@Override
public UUID generateUuid(SharedSessionContractImplementor session) { return next(); }
public synchronized UUID next() {
long now = System.currentTimeMillis();
if (now > lastMs) {
lastMs = now;
counter = RNG.nextInt(1 << 11); // start in the lower half so a burst has room to count up
} else {
counter++; // same millisecond (or the clock stepped back): keep counting
if (counter > 0xFFF) { lastMs++; counter = 0; } // counter exhausted: borrow the next millisecond
}
long msb = (lastMs << 16) | (0x7L << 12) | counter;
long lsb = (RNG.nextLong() & 0x3FFF_FFFF_FFFF_FFFFL) | 0x8000_0000_0000_0000L;
return new UUID(msb, lsb);
}
}
@@ -0,0 +1,22 @@
package com.ankurm.idgen;
import jakarta.persistence.*;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** @UuidGenerator(style = VERSION_7): RFC 9562 version 7, Unix-millisecond timestamp first. */
@Entity
public class UuidV7Item {
@Id
@UuidGenerator(style = UuidGenerator.Style.VERSION_7)
private UUID id;
private String name;
public UuidV7Item() {}
public UuidV7Item(String name) { this.name = name; }
public UUID getId() { return id; }
public String getName() { return name; }
}
@@ -0,0 +1,19 @@
package com.ankurm.idgen.spring;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import java.util.UUID;
/** Application-assigned UUID id, no @Version: Spring Data cannot tell a new instance from a detached one. */
@Entity
public class AssignedBook {
@Id
private UUID id;
private String title;
public AssignedBook() {}
public AssignedBook(UUID id, String title) { this.id = id; this.title = title; }
public UUID getId() { return id; }
}
@@ -0,0 +1,7 @@
package com.ankurm.idgen.spring;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AssignedBookRepository extends JpaRepository<AssignedBook, UUID> {
}
@@ -0,0 +1,21 @@
package com.ankurm.idgen.spring;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;
/** Generated version 7 id: id is null until persist, so Spring Data treats a fresh instance as new. */
@Entity
public class GeneratedBook {
@Id
@UuidGenerator(style = UuidGenerator.Style.VERSION_7)
private UUID id;
private String title;
public GeneratedBook() {}
public GeneratedBook(String title) { this.title = title; }
public UUID getId() { return id; }
}
@@ -0,0 +1,7 @@
package com.ankurm.idgen.spring;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
public interface GeneratedBookRepository extends JpaRepository<GeneratedBook, UUID> {
}
@@ -0,0 +1,34 @@
package com.ankurm.idgen.spring;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.PostLoad;
import jakarta.persistence.PostPersist;
import jakarta.persistence.Transient;
import java.util.UUID;
import org.springframework.data.domain.Persistable;
/** Assigned UUID id; implements Persistable so the entity itself says whether it is new. */
@Entity
public class PersistableBook implements Persistable<UUID> {
@Id
private UUID id;
private String title;
@Transient
private boolean isNew = true;
public PersistableBook() {}
public PersistableBook(UUID id, String title) { this.id = id; this.title = title; }
@Override
public UUID getId() { return id; }
@Override
public boolean isNew() { return isNew; }
@PostLoad
@PostPersist
void markNotNew() { this.isNew = false; }
}
@@ -0,0 +1,7 @@
package com.ankurm.idgen.spring;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PersistableBookRepository extends JpaRepository<PersistableBook, UUID> {
}
@@ -0,0 +1,8 @@
package com.ankurm.idgen.spring;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/** Only used by the Spring Data test; the other tests bootstrap Hibernate directly. Scans just this package. */
@SpringBootApplication
public class SpringIdApplication {
}
@@ -0,0 +1,22 @@
package com.ankurm.idgen.spring;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Version;
import java.util.UUID;
/** Assigned UUID id plus a boxed @Version: a null version tells Spring Data the instance is new, so save() calls persist. */
@Entity
public class VersionedBook {
@Id
private UUID id;
@Version
private Long version;
private String title;
public VersionedBook() {}
public VersionedBook(UUID id, String title) { this.id = id; this.title = title; }
public UUID getId() { return id; }
}
@@ -0,0 +1,7 @@
package com.ankurm.idgen.spring;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
public interface VersionedBookRepository extends JpaRepository<VersionedBook, UUID> {
}