From 3483e16657c8eb786e1a7320698ccc316d49b846 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 13 Aug 2024 11:14:01 +0200 Subject: [PATCH] Polishing. Defer user-class lookup. See #3564 --- .../support/SimpleJpaRepository.java | 19 ++++++++++--------- 1 file changed, 10 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 9ef6ba4b9..067cc8a56 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 @@ -195,13 +195,13 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation type = ProxyUtils.getUserClass(entity); - if (entityManager.contains(entity)) { entityManager.remove(entity); return; } + Class type = ProxyUtils.getUserClass(entity); + // if the entity to be deleted doesn't exist, delete is a NOOP T existing = (T) entityManager.find(type, entityInformation.getId(entity)); if (existing != null) { @@ -282,8 +282,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation implements JpaRepositoryImplementation hints = getHints(); - return Optional.ofNullable(type == null ? entityManager.find(domainType, id, hints) : entityManager.find(domainType, id, type, hints)); + return Optional.ofNullable( + type == null ? entityManager.find(domainType, id, hints) : entityManager.find(domainType, id, type, hints)); } @Deprecated @@ -481,7 +481,8 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation delete = builder.createCriteriaDelete(getDomainClass()); if (spec != null) { - Predicate predicate = spec.toPredicate(delete.from(getDomainClass()), builder.createQuery(getDomainClass()), builder); + Predicate predicate = spec.toPredicate(delete.from(getDomainClass()), builder.createQuery(getDomainClass()), + builder); if (predicate != null) { delete.where(predicate); @@ -519,7 +520,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation query = getQuery(specToUse, domainClass, sort); if (scrollPosition instanceof OffsetScrollPosition offset) { - if(!offset.isInitial()) { + if (!offset.isInitial()) { query.setFirstResult(Math.toIntExact(offset.getOffset()) + 1); } } @@ -531,8 +532,8 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation scrollDelegate = new SpecificationScrollDelegate<>(scrollFunction, entityInformation); - FetchableFluentQueryBySpecification fluentQuery = new FetchableFluentQueryBySpecification<>(spec, domainClass, finder, - scrollDelegate, this::count, this::exists, this.entityManager, getProjectionFactory()); + FetchableFluentQueryBySpecification fluentQuery = new FetchableFluentQueryBySpecification<>(spec, domainClass, + finder, scrollDelegate, this::count, this::exists, this.entityManager, getProjectionFactory()); return queryFunction.apply((FetchableFluentQuery) fluentQuery); }