Skip to content

Cache query strings in SimpleJpaRepository to improve performance #3920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
* @author Diego Krupitza
* @author Seol-JY
* @author Joshua Chen
* @author Dockerel
*/
@Repository
@Transactional(readOnly = true)
Expand All @@ -121,6 +122,9 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
private final EntityManager entityManager;
private final PersistenceProvider provider;

private final String deleteAllQueryString;
private final String countQueryString;

private @Nullable CrudMethodMetadata metadata;
private ProjectionFactory projectionFactory;
private EscapeCharacter escapeCharacter = EscapeCharacter.DEFAULT;
Expand All @@ -140,6 +144,9 @@ public SimpleJpaRepository(JpaEntityInformation<T, ?> entityInformation, EntityM
this.entityManager = entityManager;
this.provider = PersistenceProvider.fromEntityManager(entityManager);
this.projectionFactory = new SpelAwareProxyProjectionFactory();

this.deleteAllQueryString = getDeleteAllQueryString();
this.countQueryString = getCountQueryString();
}

/**
Expand Down Expand Up @@ -309,7 +316,7 @@ public void deleteAll() {
@Transactional
public void deleteAllInBatch() {

Query query = entityManager.createQuery(getDeleteAllQueryString());
Query query = entityManager.createQuery(deleteAllQueryString);

applyQueryHints(query);

Expand Down Expand Up @@ -630,7 +637,7 @@ public <S extends T, R> R findBy(Example<S> example, Function<FetchableFluentQue
@Override
public long count() {

TypedQuery<Long> query = entityManager.createQuery(getCountQueryString(), Long.class);
TypedQuery<Long> query = entityManager.createQuery(countQueryString, Long.class);

applyQueryHintsForCount(query);

Expand Down