From 09f4cef0f3600654bfe2b30f25e23fdd3dcab99a Mon Sep 17 00:00:00 2001 From: arefbehboudi Date: Tue, 17 Sep 2024 14:27:10 +0330 Subject: [PATCH] Polishing --- .../data/jpa/domain/JpaSort.java | 2 +- .../jpa/repository/aot/JpaRuntimeHints.java | 5 ++- .../JpaMetamodelEntityInformation.java | 10 +++--- .../data/jpa/repository/support/Querydsl.java | 31 ++++++------------- .../support/QuerydslJpaPredicateExecutor.java | 14 ++++----- .../support/SimpleJpaRepository.java | 14 +++++---- 6 files changed, 33 insertions(+), 43 deletions(-) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/domain/JpaSort.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/domain/JpaSort.java index e1fa003384..a28bf8a390 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/domain/JpaSort.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/domain/JpaSort.java @@ -303,7 +303,7 @@ public String toString() { builder.append(attribute.getName()).append("."); } - return builder.length() == 0 ? "" : builder.substring(0, builder.lastIndexOf(".")); + return builder.isEmpty() ? "" : builder.substring(0, builder.lastIndexOf(".")); } } diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/aot/JpaRuntimeHints.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/aot/JpaRuntimeHints.java index 955401af7d..a2dcece1f2 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/aot/JpaRuntimeHints.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/aot/JpaRuntimeHints.java @@ -88,9 +88,8 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) // streaming results requires reflective access to jakarta.persistence.Query#getResultAsStream hints.reflection().registerType(jakarta.persistence.Query.class, MemberCategory.INTROSPECT_PUBLIC_METHODS); - hints.reflection().registerType(jakarta.persistence.Query.class, hint -> { - hint.withMethod("getResultStream", Collections.emptyList(), ExecutableMode.INVOKE); - }); + hints.reflection().registerType(jakarta.persistence.Query.class, hint -> + hint.withMethod("getResultStream", Collections.emptyList(), ExecutableMode.INVOKE)); hints.reflection().registerType(NamedEntityGraph.class, hint -> hint.onReachableType(EntityGraph.class).withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java index 9979fc773b..35ced5db01 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/JpaMetamodelEntityInformation.java @@ -234,21 +234,21 @@ public Map getKeyset(Iterable propertyPaths, T entity) { Function getter = getPropertyValueFunction(entity); - Map keyset = new LinkedHashMap<>(); + Map keySet = new LinkedHashMap<>(); if (hasCompositeId()) { for (String idAttributeName : getIdAttributeNames()) { - keyset.put(idAttributeName, getter.apply(idAttributeName)); + keySet.put(idAttributeName, getter.apply(idAttributeName)); } } else { - keyset.put(getIdAttribute().getName(), getId(entity)); + keySet.put(getIdAttribute().getName(), getId(entity)); } for (String propertyPath : propertyPaths) { - keyset.put(propertyPath, getter.apply(propertyPath)); + keySet.put(propertyPath, getter.apply(propertyPath)); } - return keyset; + return keySet; } private Function getPropertyValueFunction(Object entity) { diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java index eaadddb2a3..0ade24f133 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java @@ -77,15 +77,11 @@ public Querydsl(EntityManager em, PathBuilder builder) { */ public AbstractJPAQuery> createQuery() { - switch (provider) { - case ECLIPSELINK: - return new JPAQuery<>(em, EclipseLinkTemplates.DEFAULT); - case HIBERNATE: - return new JPAQuery<>(em, HQLTemplates.DEFAULT); - case GENERIC_JPA: - default: - return new JPAQuery<>(em); - } + return switch (provider) { + case ECLIPSELINK -> new JPAQuery<>(em, EclipseLinkTemplates.DEFAULT); + case HIBERNATE -> new JPAQuery<>(em, HQLTemplates.DEFAULT); + default -> new JPAQuery<>(em); + }; } /** @@ -202,18 +198,11 @@ private NullHandling toQueryDslNullHandling(org.springframework.data.domain.Sort Assert.notNull(nullHandling, "NullHandling must not be null"); - switch (nullHandling) { - - case NULLS_FIRST: - return NullHandling.NullsFirst; - - case NULLS_LAST: - return NullHandling.NullsLast; - - case NATIVE: - default: - return NullHandling.Default; - } + return switch (nullHandling) { + case NULLS_FIRST -> NullHandling.NullsFirst; + case NULLS_LAST -> NullHandling.NullsLast; + default -> NullHandling.Default; + }; } /** diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaPredicateExecutor.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaPredicateExecutor.java index c16f95c0a1..421fbc35f0 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaPredicateExecutor.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaPredicateExecutor.java @@ -192,15 +192,15 @@ public R findBy(Predicate predicate, Function implements JpaRepositoryImplementation { private static final String ID_MUST_NOT_BE_NULL = "The given id must not be null"; + private static final String IDS_MUST_NOT_BE_NULL = "Ids must not be null"; + private static final String ENTITIES_MUST_NOT_BE_NULL = "Entities must not be null"; private final JpaEntityInformation entityInformation; private final EntityManager entityManager; @@ -212,7 +214,7 @@ public void delete(T entity) { @Transactional public void deleteAllById(Iterable ids) { - Assert.notNull(ids, "Ids must not be null"); + Assert.notNull(ids, IDS_MUST_NOT_BE_NULL); for (ID id : ids) { deleteById(id); @@ -223,7 +225,7 @@ public void deleteAllById(Iterable ids) { @Transactional public void deleteAllByIdInBatch(Iterable ids) { - Assert.notNull(ids, "Ids must not be null"); + Assert.notNull(ids, IDS_MUST_NOT_BE_NULL); if (!ids.iterator().hasNext()) { return; @@ -258,7 +260,7 @@ public void deleteAllByIdInBatch(Iterable ids) { @Transactional public void deleteAll(Iterable entities) { - Assert.notNull(entities, "Entities must not be null"); + Assert.notNull(entities, ENTITIES_MUST_NOT_BE_NULL); for (T entity : entities) { delete(entity); @@ -269,7 +271,7 @@ public void deleteAll(Iterable entities) { @Transactional public void deleteAllInBatch(Iterable entities) { - Assert.notNull(entities, "Entities must not be null"); + Assert.notNull(entities, ENTITIES_MUST_NOT_BE_NULL); if (!entities.iterator().hasNext()) { return; @@ -390,7 +392,7 @@ public List findAll() { @Override public List findAllById(Iterable ids) { - Assert.notNull(ids, "Ids must not be null"); + Assert.notNull(ids, IDS_MUST_NOT_BE_NULL); if (!ids.iterator().hasNext()) { return Collections.emptyList(); @@ -639,7 +641,7 @@ public S saveAndFlush(S entity) { @Transactional public List saveAll(Iterable entities) { - Assert.notNull(entities, "Entities must not be null"); + Assert.notNull(entities, ENTITIES_MUST_NOT_BE_NULL); List result = new ArrayList<>();