Skip to content

Commit 0dcc0c2

Browse files
Dockerelmp911de
authored andcommitted
Cache query strings in SimpleJpaRepository.
Cache the deleteAll and count query strings as final fields in SimpleJpaRepository. This avoids repeated String.format operations and reduces unnecessary object creation on every invocation of deleteAllInBatch() and count(). No functional changes. Signed-off-by: Giheon Do <dgh0001@naver.com> Closes #3920
1 parent 351dc07 commit 0dcc0c2

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
* @author Diego Krupitza
105105
* @author Seol-JY
106106
* @author Joshua Chen
107+
* @author Dockerel
107108
*/
108109
@Repository
109110
@Transactional(readOnly = true)
@@ -121,6 +122,9 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
121122
private final EntityManager entityManager;
122123
private final PersistenceProvider provider;
123124

125+
private final String deleteAllQueryString;
126+
private final String countQueryString;
127+
124128
private @Nullable CrudMethodMetadata metadata;
125129
private ProjectionFactory projectionFactory;
126130
private EscapeCharacter escapeCharacter = EscapeCharacter.DEFAULT;
@@ -140,6 +144,9 @@ public SimpleJpaRepository(JpaEntityInformation<T, ?> entityInformation, EntityM
140144
this.entityManager = entityManager;
141145
this.provider = PersistenceProvider.fromEntityManager(entityManager);
142146
this.projectionFactory = new SpelAwareProxyProjectionFactory();
147+
148+
this.deleteAllQueryString = getDeleteAllQueryString();
149+
this.countQueryString = getCountQueryString();
143150
}
144151

145152
/**
@@ -318,7 +325,7 @@ public void deleteAll() {
318325
@Transactional
319326
public void deleteAllInBatch() {
320327

321-
Query query = entityManager.createQuery(getDeleteAllQueryString());
328+
Query query = entityManager.createQuery(deleteAllQueryString);
322329

323330
applyQueryHints(query);
324331

@@ -639,7 +646,7 @@ public <S extends T, R> R findBy(Example<S> example, Function<FetchableFluentQue
639646
@Override
640647
public long count() {
641648

642-
TypedQuery<Long> query = entityManager.createQuery(getCountQueryString(), Long.class);
649+
TypedQuery<Long> query = entityManager.createQuery(countQueryString, Long.class);
643650

644651
applyQueryHintsForCount(query);
645652

0 commit comments

Comments
 (0)