From 928c2052047ef4cd9127ba50e01c1d14dd2ac70a Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Wed, 8 May 2024 10:37:53 +0800 Subject: [PATCH] Polishing `SimpleJpaRepository` 1. `TypedQuery.set*()` returns the same query instance 2. remove `if (spec != null)` since `spec` must not be null --- .../support/SimpleJpaRepository.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 50afeee8a5c..eb6f2938cd6 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -244,7 +244,7 @@ public void deleteAllByIdInBatch(Iterable ids) { * Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already. */ - if (Collection.class.isInstance(ids)) { + if (ids instanceof Collection) { query.setParameter("ids", ids); } else { Collection idsCollection = StreamSupport.stream(ids.spliterator(), false) @@ -481,12 +481,10 @@ public long delete(Specification spec) { CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); CriteriaDelete delete = builder.createCriteriaDelete(getDomainClass()); - if (spec != null) { - Predicate predicate = spec.toPredicate(delete.from(getDomainClass()), null, builder); + Predicate predicate = spec.toPredicate(delete.from(getDomainClass()), builder.createQuery(getDomainClass()), builder); - if (predicate != null) { - delete.where(predicate); - } + if (predicate != null) { + delete.where(predicate); } return this.entityManager.createQuery(delete).executeUpdate(); @@ -853,11 +851,13 @@ private TypedQuery applyRepositoryMethodMetadata(TypedQuery query) { } LockModeType type = metadata.getLockModeType(); - TypedQuery toReturn = type == null ? query : query.setLockMode(type); + if (type == null) { + query.setLockMode(type); + } - applyQueryHints(toReturn); + applyQueryHints(query); - return toReturn; + return query; } private void applyQueryHints(Query query) {