Skip to content

Commit a4fe9c3

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 <[email protected]> Closes #3920
1 parent f635de7 commit a4fe9c3

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
@@ -103,6 +103,7 @@
103103
* @author Diego Krupitza
104104
* @author Seol-JY
105105
* @author Joshua Chen
106+
* @author Dockerel
106107
*/
107108
@Repository
108109
@Transactional(readOnly = true)
@@ -120,6 +121,9 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
120121
private final EntityManager entityManager;
121122
private final PersistenceProvider provider;
122123

124+
private final String deleteAllQueryString;
125+
private final String countQueryString;
126+
123127
private @Nullable CrudMethodMetadata metadata;
124128
private ProjectionFactory projectionFactory;
125129
private EscapeCharacter escapeCharacter = EscapeCharacter.DEFAULT;
@@ -139,6 +143,9 @@ public SimpleJpaRepository(JpaEntityInformation<T, ?> entityInformation, EntityM
139143
this.entityManager = entityManager;
140144
this.provider = PersistenceProvider.fromEntityManager(entityManager);
141145
this.projectionFactory = new SpelAwareProxyProjectionFactory();
146+
147+
this.deleteAllQueryString = getDeleteAllQueryString();
148+
this.countQueryString = getCountQueryString();
142149
}
143150

144151
/**
@@ -309,7 +316,7 @@ public void deleteAll() {
309316
@Transactional
310317
public void deleteAllInBatch() {
311318

312-
Query query = entityManager.createQuery(getDeleteAllQueryString());
319+
Query query = entityManager.createQuery(deleteAllQueryString);
313320

314321
applyQueryHints(query);
315322

@@ -631,7 +638,7 @@ public <S extends T, R> R findBy(Example<S> example, Function<FetchableFluentQue
631638
@Override
632639
public long count() {
633640

634-
TypedQuery<Long> query = entityManager.createQuery(getCountQueryString(), Long.class);
641+
TypedQuery<Long> query = entityManager.createQuery(countQueryString, Long.class);
635642

636643
applyQueryHintsForCount(query);
637644

0 commit comments

Comments
 (0)