Add AuthorSpecifications: PredicateSpecification/DeleteSpecification/UpdateSpecification helpers
This commit is contained in:
@@ -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<Author> hasCountry(String country) {
|
||||
return (root, cb) -> cb.equal(root.get("country"), country);
|
||||
}
|
||||
|
||||
// DeleteSpecification: explicit CriteriaDelete-typed specification for a bulk delete.
|
||||
public static DeleteSpecification<Author> 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<Author> relabelCountry(String from, String to) {
|
||||
return UpdateSpecification
|
||||
.<Author>update((root, update, cb) -> update.set(root.get("country"), to))
|
||||
.where(hasCountry(from));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user