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,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> {
}