Skip to content
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

Use provider built-in result count to reuse query if possible #3456

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Use provider built-in result count to reuse query if possible
Hibernate introduce `SelectionQuery::getResultCount` since 6.5.0, It may be JPA Query method in the future.
  • Loading branch information
quaff committed Apr 29, 2024
commit d73a86de7be55589ee3c064b37e781353e0bba28
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
* @author Сергей Цыпанов
* @author Wonchul Heo
* @author Julia Lee
* @author Yanming Zhou
*/
public abstract class AbstractJpaQuery implements RepositoryQuery {

Expand Down Expand Up @@ -99,7 +100,7 @@ public AbstractJpaQuery(JpaQueryMethod method, EntityManager em) {
} else if (method.isSliceQuery()) {
return new SlicedExecution();
} else if (method.isPageQuery()) {
return new PagedExecution();
return new PagedExecution(em);
} else if (method.isModifyingQuery()) {
return null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.LongSupplier;

import org.hibernate.query.SelectionQuery;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
Expand Down Expand Up @@ -57,6 +59,7 @@
* @author Jens Schauder
* @author Gabriel Basilio
* @author Greg Turnquist
* @author Yanming Zhou
*/
public abstract class JpaQueryExecution {

Expand Down Expand Up @@ -195,14 +198,30 @@ protected Object doExecute(AbstractJpaQuery query, JpaParametersParameterAccesso
*/
static class PagedExecution extends JpaQueryExecution {

private final EntityManager em;

public PagedExecution(EntityManager em) {

Assert.notNull(em, "The EntityManager must not be null");
this.em = em;
}

@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor accessor) {

Query query = repositoryQuery.createQuery(accessor);

return PageableExecutionUtils.getPage(query.getResultList(), accessor.getPageable(),
() -> count(repositoryQuery, accessor));
LongSupplier count = () -> {
boolean userDefinedCountQuery = (repositoryQuery instanceof AbstractStringBasedJpaQuery);
if (!userDefinedCountQuery && PersistenceProvider.fromEntityManager(em) == PersistenceProvider.HIBERNATE) {
// Hibernate introduce SelectionQuery::getResultCount since 6.5.0
return ((SelectionQuery<?>) query).getResultCount();
}
return count(repositoryQuery, accessor);
};

return PageableExecutionUtils.getPage(query.getResultList(), accessor.getPageable(), count);
}

private long count(AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor accessor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public static void sampleMethod(Pageable pageable) {}
@BeforeEach
void setUp() {

when(em.getDelegate()).thenReturn(em); // for PersistenceProvider.fromEntityManager(em)
when(query.executeUpdate()).thenReturn(0);
when(jpaQuery.createQuery(Mockito.any(JpaParametersParameterAccessor.class))).thenReturn(query);
when(jpaQuery.getQueryMethod()).thenReturn(method);
Expand Down Expand Up @@ -183,7 +184,7 @@ void pagedExecutionRetrievesObjectsForPageableOutOfRange() throws Exception {
when(jpaQuery.createQuery(Mockito.any())).thenReturn(query);
when(countQuery.getResultList()).thenReturn(Arrays.asList(20L));

PagedExecution execution = new PagedExecution();
PagedExecution execution = new PagedExecution(em);
execution.doExecute(jpaQuery,
new JpaParametersParameterAccessor(parameters, new Object[] { PageRequest.of(2, 10) }));

Expand All @@ -199,7 +200,7 @@ void pagedExecutionShouldNotGenerateCountQueryIfQueryReportedNoResults() throws
when(jpaQuery.createQuery(Mockito.any())).thenReturn(query);
when(query.getResultList()).thenReturn(Arrays.asList(0L));

PagedExecution execution = new PagedExecution();
PagedExecution execution = new PagedExecution(em);
execution.doExecute(jpaQuery,
new JpaParametersParameterAccessor(parameters, new Object[] { PageRequest.of(0, 10) }));

Expand All @@ -215,7 +216,7 @@ void pagedExecutionShouldUseCountFromResultIfOffsetIsZeroAndResultsWithinPageSiz
when(jpaQuery.createQuery(Mockito.any())).thenReturn(query);
when(query.getResultList()).thenReturn(Arrays.asList(new Object(), new Object(), new Object(), new Object()));

PagedExecution execution = new PagedExecution();
PagedExecution execution = new PagedExecution(em);
execution.doExecute(jpaQuery,
new JpaParametersParameterAccessor(parameters, new Object[] { PageRequest.of(0, 10) }));

Expand All @@ -230,7 +231,7 @@ void pagedExecutionShouldUseCountFromResultWithOffsetAndResultsWithinPageSize()
when(jpaQuery.createQuery(Mockito.any())).thenReturn(query);
when(query.getResultList()).thenReturn(Arrays.asList(new Object(), new Object(), new Object(), new Object()));

PagedExecution execution = new PagedExecution();
PagedExecution execution = new PagedExecution(em);
execution.doExecute(jpaQuery,
new JpaParametersParameterAccessor(parameters, new Object[] { PageRequest.of(5, 10) }));

Expand All @@ -247,7 +248,7 @@ void pagedExecutionShouldUseRequestCountFromResultWithOffsetAndResultsHitLowerPa
when(jpaQuery.createCountQuery(Mockito.any())).thenReturn(query);
when(countQuery.getResultList()).thenReturn(Arrays.asList(20L));

PagedExecution execution = new PagedExecution();
PagedExecution execution = new PagedExecution(em);
execution.doExecute(jpaQuery,
new JpaParametersParameterAccessor(parameters, new Object[] { PageRequest.of(4, 4) }));

Expand All @@ -264,7 +265,7 @@ void pagedExecutionShouldUseRequestCountFromResultWithOffsetAndResultsHitUpperPa
when(jpaQuery.createCountQuery(Mockito.any())).thenReturn(query);
when(countQuery.getResultList()).thenReturn(Arrays.asList(20L));

PagedExecution execution = new PagedExecution();
PagedExecution execution = new PagedExecution(em);
execution.doExecute(jpaQuery,
new JpaParametersParameterAccessor(parameters, new Object[] { PageRequest.of(4, 4) }));

Expand Down
Loading