Skip to content

Commit

Permalink
[#1965] Use regular CriteriaBuilder instead of PaginatedCriteriaBuild…
Browse files Browse the repository at this point in the history
…er for unpaged Spring Data JPA repository invocations
  • Loading branch information
Mobe91 committed Dec 12, 2024
1 parent e16c5c0 commit 24c05e0
Show file tree
Hide file tree
Showing 24 changed files with 291 additions and 221 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ For executing tests against a database on a dedicated host you might want to spe
## Testing with Jakarta Persistence provider

To build everything use `mvn -pl core/testsuite-jakarta-runner clean install -am -P "hibernate-6.2,jakarta,h2,spring-data-2.6.x,deltaspike-1.9" -DskipTests`
and to run tests use ` mvn -pl core/testsuite-jakarta-runner clean install -P "hibernate-6.2,jakarta,h2,spring-data-2.6.x,deltaspike-1.9" "-Dtest=com.blazebit.persistence.testsuite.SetOperationTest#testUnionAllOrderBySubqueryLimit"`.
and to run tests use `mvn -pl core/testsuite-jakarta-runner clean install -P "hibernate-6.2,jakarta,h2,spring-data-2.6.x,deltaspike-1.9" "-Dtest=com.blazebit.persistence.testsuite.SetOperationTest#testUnionAllOrderBySubqueryLimit"`.

## Switching JPA provider profiles in IntelliJ

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ protected ParameterMetadataProvider createParameterMetadataProvider(CriteriaBuil
@Override
protected JpaQueryExecution getExecution() {
if (getQueryMethod().isSliceQuery()) {
return new PartTreeBlazePersistenceQuery.SlicedExecution(getQueryMethod().getParameters());
return new PartTreeBlazePersistenceQuery.SlicedExecution();
} else if (getQueryMethod().isPageQuery()) {
return new PartTreeBlazePersistenceQuery.PagedExecution(getQueryMethod().getParameters());
return new PartTreeBlazePersistenceQuery.PagedExecution();
} else if (isDelete()) {
return new PartTreeBlazePersistenceQuery.DeleteExecution(getEntityManager());
} else if (isExists()) {
Expand Down Expand Up @@ -93,18 +93,12 @@ protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
*/
private static class SlicedExecution extends JpaQueryExecution {

private final Parameters<?, ?> parameters;

public SlicedExecution(Parameters<?, ?> parameters) {
this.parameters = parameters;
}

@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
Query paginatedCriteriaBuilder = ((PartTreeBlazePersistenceQuery) repositoryQuery).createPaginatedQuery(values, false);
PagedList<Object> resultList = (PagedList<Object>) paginatedCriteriaBuilder.getResultList();
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values);
ParameterAccessor accessor = new ParametersParameterAccessor(repositoryQuery.getQueryMethod().getParameters(), values);
Pageable pageable = accessor.getPageable();

return new KeysetAwareSliceImpl<>(resultList, pageable);
Expand All @@ -119,20 +113,19 @@ protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
*/
private static class PagedExecution extends JpaQueryExecution {

private final Parameters<?, ?> parameters;

public PagedExecution(Parameters<?, ?> parameters) {
this.parameters = parameters;
}

@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
ParameterAccessor accessor = new ParametersParameterAccessor(repositoryQuery.getQueryMethod().getParameters(), values);
Pageable pageable = accessor.getPageable();
if (pageable == null) {
List<Object> unpagedResult = ((PartTreeBlazePersistenceQuery) repositoryQuery).createQuery(values).getResultList();
return new KeysetAwarePageImpl<>(unpagedResult);
}

Query paginatedCriteriaBuilder = ((PartTreeBlazePersistenceQuery) repositoryQuery).createPaginatedQuery(values, true);
PagedList<Object> resultList = (PagedList<Object>) paginatedCriteriaBuilder.getResultList();
Long total = resultList.getTotalSize();
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values);
Pageable pageable = accessor.getPageable();

if (total.equals(0L)) {
return new KeysetAwarePageImpl<>(Collections.emptyList(), total, null, pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.springframework.data.jpa.repository.query.JpaEntityGraph;
import org.springframework.data.jpa.repository.query.JpaQueryExecution;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.repository.query.parser.PartTree;

Expand Down Expand Up @@ -59,9 +58,9 @@ protected ParameterMetadataProvider createParameterMetadataProvider(CriteriaBuil
@Override
protected JpaQueryExecution getExecution() {
if (getQueryMethod().isSliceQuery()) {
return new PartTreeBlazePersistenceQuery.SlicedExecution(getQueryMethod().getParameters());
return new PartTreeBlazePersistenceQuery.SlicedExecution();
} else if (getQueryMethod().isPageQuery()) {
return new PartTreeBlazePersistenceQuery.PagedExecution(getQueryMethod().getParameters());
return new PartTreeBlazePersistenceQuery.PagedExecution();
} else if (isDelete()) {
return new PartTreeBlazePersistenceQuery.DeleteExecution(getEntityManager());
} else if (isExists()) {
Expand Down Expand Up @@ -93,18 +92,12 @@ protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
*/
private static class SlicedExecution extends JpaQueryExecution {

private final Parameters<?, ?> parameters;

public SlicedExecution(Parameters<?, ?> parameters) {
this.parameters = parameters;
}

@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
Query paginatedCriteriaBuilder = ((PartTreeBlazePersistenceQuery) repositoryQuery).createPaginatedQuery(values, false);
PagedList<Object> resultList = (PagedList<Object>) paginatedCriteriaBuilder.getResultList();
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values);
ParameterAccessor accessor = new ParametersParameterAccessor(repositoryQuery.getQueryMethod().getParameters(), values);
Pageable pageable = accessor.getPageable();

return new KeysetAwareSliceImpl<>(resultList, pageable);
Expand All @@ -119,20 +112,19 @@ protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
*/
private static class PagedExecution extends JpaQueryExecution {

private final Parameters<?, ?> parameters;

public PagedExecution(Parameters<?, ?> parameters) {
this.parameters = parameters;
}

@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
ParameterAccessor accessor = new ParametersParameterAccessor(repositoryQuery.getQueryMethod().getParameters(), values);
Pageable pageable = accessor.getPageable();
if (pageable.isUnpaged()) {
List<Object> unpagedResult = ((PartTreeBlazePersistenceQuery) repositoryQuery).createQuery(values).getResultList();
return new KeysetAwarePageImpl<>(unpagedResult);
}

Query paginatedCriteriaBuilder = ((PartTreeBlazePersistenceQuery) repositoryQuery).createPaginatedQuery(values, true);
PagedList<Object> resultList = (PagedList<Object>) paginatedCriteriaBuilder.getResultList();
Long total = resultList.getTotalSize();
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values);
Pageable pageable = accessor.getPageable();

if (total.equals(0L)) {
return new KeysetAwarePageImpl<>(Collections.emptyList(), total, null, pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.springframework.data.jpa.repository.query.JpaEntityGraph;
import org.springframework.data.jpa.repository.query.JpaQueryExecution;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.repository.query.parser.PartTree;

Expand Down Expand Up @@ -60,9 +59,9 @@ protected ParameterMetadataProvider createParameterMetadataProvider(CriteriaBuil
@Override
protected JpaQueryExecution getExecution() {
if (getQueryMethod().isSliceQuery()) {
return new PartTreeBlazePersistenceQuery.SlicedExecution(getQueryMethod().getParameters());
return new PartTreeBlazePersistenceQuery.SlicedExecution();
} else if (getQueryMethod().isPageQuery()) {
return new PartTreeBlazePersistenceQuery.PagedExecution(getQueryMethod().getParameters());
return new PartTreeBlazePersistenceQuery.PagedExecution();
} else if (isDelete()) {
return new PartTreeBlazePersistenceQuery.DeleteExecution(getEntityManager());
} else if (isExists()) {
Expand Down Expand Up @@ -94,18 +93,12 @@ protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
*/
private static class SlicedExecution extends JpaQueryExecution {

private final Parameters<?, ?> parameters;

public SlicedExecution(Parameters<?, ?> parameters) {
this.parameters = parameters;
}

@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
Query paginatedCriteriaBuilder = ((PartTreeBlazePersistenceQuery) repositoryQuery).createPaginatedQuery(values, false);
PagedList<Object> resultList = (PagedList<Object>) paginatedCriteriaBuilder.getResultList();
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values);
ParameterAccessor accessor = new ParametersParameterAccessor(repositoryQuery.getQueryMethod().getParameters(), values);
Pageable pageable = accessor.getPageable();

return new KeysetAwareSliceImpl<>(resultList, pageable);
Expand All @@ -120,20 +113,19 @@ protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
*/
private static class PagedExecution extends JpaQueryExecution {

private final Parameters<?, ?> parameters;

public PagedExecution(Parameters<?, ?> parameters) {
this.parameters = parameters;
}

@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
ParameterAccessor accessor = new ParametersParameterAccessor(repositoryQuery.getQueryMethod().getParameters(), values);
Pageable pageable = accessor.getPageable();
if (pageable.isUnpaged()) {
List<Object> unpagedResult = ((PartTreeBlazePersistenceQuery) repositoryQuery).createQuery(values).getResultList();
return new KeysetAwarePageImpl<>(unpagedResult);
}

Query paginatedCriteriaBuilder = ((PartTreeBlazePersistenceQuery) repositoryQuery).createPaginatedQuery(values, true);
PagedList<Object> resultList = (PagedList<Object>) paginatedCriteriaBuilder.getResultList();
Long total = resultList.getTotalSize();
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, values);
Pageable pageable = accessor.getPageable();

if (total.equals(0L)) {
return new KeysetAwarePageImpl<>(Collections.emptyList(), total, null, pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.springframework.data.jpa.repository.query.JpaEntityGraph;
import org.springframework.data.jpa.repository.query.JpaParametersParameterAccessor;
import org.springframework.data.jpa.repository.query.JpaQueryExecution;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.repository.query.parser.PartTree;

Expand Down Expand Up @@ -61,9 +59,9 @@ protected ParameterMetadataProvider createParameterMetadataProvider(CriteriaBuil
@Override
protected JpaQueryExecution getExecution() {
if (getQueryMethod().isSliceQuery()) {
return new PartTreeBlazePersistenceQuery.SlicedExecution(getQueryMethod().getParameters());
return new PartTreeBlazePersistenceQuery.SlicedExecution();
} else if (getQueryMethod().isPageQuery()) {
return new PartTreeBlazePersistenceQuery.PagedExecution(getQueryMethod().getParameters());
return new PartTreeBlazePersistenceQuery.PagedExecution();
} else if (isDelete()) {
return new PartTreeBlazePersistenceQuery.DeleteExecution(getEntityManager());
} else if (isExists()) {
Expand Down Expand Up @@ -95,19 +93,12 @@ protected Object doExecute(AbstractJpaQuery repositoryQuery, JpaParametersParame
*/
private static class SlicedExecution extends JpaQueryExecution {

private final Parameters<?, ?> parameters;

public SlicedExecution(Parameters<?, ?> parameters) {
this.parameters = parameters;
}

@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor jpaParametersParameterAccessor) {
Query paginatedCriteriaBuilder = ((PartTreeBlazePersistenceQuery) repositoryQuery).createPaginatedQuery(jpaParametersParameterAccessor.getValues(), false);
PagedList<Object> resultList = (PagedList<Object>) paginatedCriteriaBuilder.getResultList();
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, jpaParametersParameterAccessor.getValues());
Pageable pageable = accessor.getPageable();
Pageable pageable = jpaParametersParameterAccessor.getPageable();

return new KeysetAwareSliceImpl<>(resultList, pageable);
}
Expand All @@ -121,20 +112,18 @@ protected Object doExecute(AbstractJpaQuery repositoryQuery, JpaParametersParame
*/
private static class PagedExecution extends JpaQueryExecution {

private final Parameters<?, ?> parameters;

public PagedExecution(Parameters<?, ?> parameters) {
this.parameters = parameters;
}

@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor jpaParametersParameterAccessor) {
Pageable pageable = jpaParametersParameterAccessor.getPageable();
if (pageable.isUnpaged()) {
List<Object> unpagedResult = ((PartTreeBlazePersistenceQuery) repositoryQuery).createQuery(jpaParametersParameterAccessor).getResultList();
return new KeysetAwarePageImpl<>(unpagedResult);
}

Query paginatedCriteriaBuilder = ((PartTreeBlazePersistenceQuery) repositoryQuery).createPaginatedQuery(jpaParametersParameterAccessor.getValues(), true);
PagedList<Object> resultList = (PagedList<Object>) paginatedCriteriaBuilder.getResultList();
Long total = resultList.getTotalSize();
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, jpaParametersParameterAccessor.getValues());
Pageable pageable = accessor.getPageable();

if (total.equals(0L)) {
return new KeysetAwarePageImpl<>(Collections.emptyList(), total, null, pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.springframework.data.jpa.repository.query.JpaEntityGraph;
import org.springframework.data.jpa.repository.query.JpaParametersParameterAccessor;
import org.springframework.data.jpa.repository.query.JpaQueryExecution;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.repository.query.parser.PartTree;

Expand Down Expand Up @@ -61,9 +59,9 @@ protected ParameterMetadataProvider createParameterMetadataProvider(CriteriaBuil
@Override
protected JpaQueryExecution getExecution() {
if (getQueryMethod().isSliceQuery()) {
return new PartTreeBlazePersistenceQuery.SlicedExecution(getQueryMethod().getParameters());
return new PartTreeBlazePersistenceQuery.SlicedExecution();
} else if (getQueryMethod().isPageQuery()) {
return new PartTreeBlazePersistenceQuery.PagedExecution(getQueryMethod().getParameters());
return new PartTreeBlazePersistenceQuery.PagedExecution();
} else if (isDelete()) {
return new PartTreeBlazePersistenceQuery.DeleteExecution(getEntityManager());
} else if (isExists()) {
Expand Down Expand Up @@ -95,19 +93,12 @@ protected Object doExecute(AbstractJpaQuery repositoryQuery, JpaParametersParame
*/
private static class SlicedExecution extends JpaQueryExecution {

private final Parameters<?, ?> parameters;

public SlicedExecution(Parameters<?, ?> parameters) {
this.parameters = parameters;
}

@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor jpaParametersParameterAccessor) {
Query paginatedCriteriaBuilder = ((PartTreeBlazePersistenceQuery) repositoryQuery).createPaginatedQuery(jpaParametersParameterAccessor.getValues(), false);
PagedList<Object> resultList = (PagedList<Object>) paginatedCriteriaBuilder.getResultList();
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, jpaParametersParameterAccessor.getValues());
Pageable pageable = accessor.getPageable();
Pageable pageable = jpaParametersParameterAccessor.getPageable();

return new KeysetAwareSliceImpl<>(resultList, pageable);
}
Expand All @@ -121,20 +112,18 @@ protected Object doExecute(AbstractJpaQuery repositoryQuery, JpaParametersParame
*/
private static class PagedExecution extends JpaQueryExecution {

private final Parameters<?, ?> parameters;

public PagedExecution(Parameters<?, ?> parameters) {
this.parameters = parameters;
}

@Override
@SuppressWarnings("unchecked")
protected Object doExecute(AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor jpaParametersParameterAccessor) {
Pageable pageable = jpaParametersParameterAccessor.getPageable();
if (pageable.isUnpaged()) {
List<Object> unpagedResult = ((PartTreeBlazePersistenceQuery) repositoryQuery).createQuery(jpaParametersParameterAccessor).getResultList();
return new KeysetAwarePageImpl<>(unpagedResult);
}

Query paginatedCriteriaBuilder = ((PartTreeBlazePersistenceQuery) repositoryQuery).createPaginatedQuery(jpaParametersParameterAccessor.getValues(), true);
PagedList<Object> resultList = (PagedList<Object>) paginatedCriteriaBuilder.getResultList();
Long total = resultList.getTotalSize();
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, jpaParametersParameterAccessor.getValues());
Pageable pageable = accessor.getPageable();

if (total.equals(0L)) {
return new KeysetAwarePageImpl<>(Collections.emptyList(), total, null, pageable);
Expand Down
Loading

0 comments on commit 24c05e0

Please sign in to comment.