From 68b40c903b1875000f72c06e673f775adc4b54a4 Mon Sep 17 00:00:00 2001 From: asmhatre Date: Sat, 25 Jul 2026 08:18:37 +0000 Subject: [PATCH] Add AuthorSpecifications: PredicateSpecification/DeleteSpecification/UpdateSpecification helpers --- .../sdjpa4demo/repo/AuthorSpecifications.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/com/ankurm/sdjpa4demo/repo/AuthorSpecifications.java diff --git a/src/main/java/com/ankurm/sdjpa4demo/repo/AuthorSpecifications.java b/src/main/java/com/ankurm/sdjpa4demo/repo/AuthorSpecifications.java new file mode 100644 index 0000000..f0b2905 --- /dev/null +++ b/src/main/java/com/ankurm/sdjpa4demo/repo/AuthorSpecifications.java @@ -0,0 +1,36 @@ +package com.ankurm.sdjpa4demo.repo; + +import com.ankurm.sdjpa4demo.domain.Author; +import org.springframework.data.jpa.domain.DeleteSpecification; +import org.springframework.data.jpa.domain.PredicateSpecification; +import org.springframework.data.jpa.domain.UpdateSpecification; + +/** + * Reusable Specification-family helpers for Author, exercising the refined Specification API + * introduced in Spring Data JPA 4.0: {@link PredicateSpecification} (context-agnostic, reusable + * across select/update/delete), {@link DeleteSpecification} (CriteriaDelete-backed bulk delete), + * and {@link UpdateSpecification} (CriteriaUpdate-backed bulk update). + */ +public final class AuthorSpecifications { + + private AuthorSpecifications() { } + + // PredicateSpecification: a bare Predicate, usable regardless of whether the surrounding + // query is a select, update, or delete - this exact instance is reused for both below. + public static PredicateSpecification hasCountry(String country) { + return (root, cb) -> cb.equal(root.get("country"), country); + } + + // DeleteSpecification: explicit CriteriaDelete-typed specification for a bulk delete. + public static DeleteSpecification byCountryDelete(String country) { + return (root, delete, cb) -> cb.equal(root.get("country"), country); + } + + // UpdateSpecification: composes an UpdateOperation (the SET clause, over CriteriaUpdate) + // with a PredicateSpecification (the WHERE clause) via UpdateSpecification.update(...).where(...). + public static UpdateSpecification relabelCountry(String from, String to) { + return UpdateSpecification + .update((root, update, cb) -> update.set(root.get("country"), to)) + .where(hasCountry(from)); + } +}