Skip to content

Fluent query API should use ProjectionFactory provided by the RepositoryFactory #3432

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3410-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3410-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3410-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3410-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3410-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-3410-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ public interface CrudMethodMetadata {
* @since 1.9
*/
Method getMethod();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JpaRepositoryImplementation provides already customization options for repository implementations. What if we evolve that concept by moving setRepositoryMethodMetadata and setEscapeCharacter methods into a new interface, that would also accept ProjectionFactory and let both, JpaRepositoryImplementation and QuerydslJpaPredicateExecutor implement that interface?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth exploring - also in the light of having to deal with a similar arrangement in other store modules.

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanClassLoaderAware;
Expand Down Expand Up @@ -274,6 +275,7 @@ public Optional<EntityGraph> getEntityGraph() {
public Method getMethod() {
return method;
}

}

private static class ThreadBoundTargetSource implements TargetSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Window;
import org.springframework.data.jpa.repository.query.ScrollDelegate;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.util.Assert;
Expand All @@ -51,6 +52,7 @@
* @author Mark Paluch
* @author Jens Schauder
* @author J.R. Onyschak
* @author Christoph Strobl
* @since 2.6
*/
class FetchableFluentQueryByPredicate<S, R> extends FluentQuerySupport<S, R> implements FetchableFluentQuery<R> {
Expand All @@ -64,21 +66,21 @@ class FetchableFluentQueryByPredicate<S, R> extends FluentQuerySupport<S, R> imp
private final Function<Predicate, Boolean> existsOperation;
private final EntityManager entityManager;

public FetchableFluentQueryByPredicate(Predicate predicate, Class<S> entityType,
FetchableFluentQueryByPredicate(Predicate predicate, Class<S> entityType,
Function<Sort, AbstractJPAQuery<?, ?>> finder, PredicateScrollDelegate<S> scroll,
BiFunction<Sort, Pageable, AbstractJPAQuery<?, ?>> pagedFinder, Function<Predicate, Long> countOperation,
Function<Predicate, Boolean> existsOperation, EntityManager entityManager) {
Function<Predicate, Boolean> existsOperation, EntityManager entityManager, ProjectionFactory projectionFactory) {
this(predicate, entityType, (Class<R>) entityType, Sort.unsorted(), 0, Collections.emptySet(), finder, scroll,
pagedFinder, countOperation, existsOperation, entityManager);
pagedFinder, countOperation, existsOperation, entityManager, projectionFactory);
}

private FetchableFluentQueryByPredicate(Predicate predicate, Class<S> entityType, Class<R> resultType, Sort sort,
int limit, Collection<String> properties, Function<Sort, AbstractJPAQuery<?, ?>> finder,
PredicateScrollDelegate<S> scroll, BiFunction<Sort, Pageable, AbstractJPAQuery<?, ?>> pagedFinder,
Function<Predicate, Long> countOperation, Function<Predicate, Boolean> existsOperation,
EntityManager entityManager) {
EntityManager entityManager, ProjectionFactory projectionFactory) {

super(resultType, sort, limit, properties, entityType);
super(resultType, sort, limit, properties, entityType, projectionFactory);
this.predicate = predicate;
this.finder = finder;
this.scroll = scroll;
Expand All @@ -94,7 +96,7 @@ public FetchableFluentQuery<R> sortBy(Sort sort) {
Assert.notNull(sort, "Sort must not be null");

return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, this.sort.and(sort), limit,
properties, finder, scroll, pagedFinder, countOperation, existsOperation, entityManager);
properties, finder, scroll, pagedFinder, countOperation, existsOperation, entityManager, projectionFactory);
}

@Override
Expand All @@ -103,7 +105,7 @@ public FetchableFluentQuery<R> limit(int limit) {
Assert.isTrue(limit >= 0, "Limit must not be negative");

return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, sort, limit, properties, finder,
scroll, pagedFinder, countOperation, existsOperation, entityManager);
scroll, pagedFinder, countOperation, existsOperation, entityManager, projectionFactory);
}

@Override
Expand All @@ -116,14 +118,15 @@ public <NR> FetchableFluentQuery<NR> as(Class<NR> resultType) {
}

return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, sort, limit, properties, finder,
scroll, pagedFinder, countOperation, existsOperation, entityManager);
scroll, pagedFinder, countOperation, existsOperation, entityManager, projectionFactory);
}

@Override
public FetchableFluentQuery<R> project(Collection<String> properties) {

return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, sort, limit,
mergeProperties(properties), finder, scroll, pagedFinder, countOperation, existsOperation, entityManager);
mergeProperties(properties), finder, scroll, pagedFinder, countOperation, existsOperation, entityManager,
projectionFactory);
}

@Override
Expand Down Expand Up @@ -230,7 +233,6 @@ private Function<Object, R> getConversionFunction() {
return getConversionFunction(entityType, resultType);
}


static class PredicateScrollDelegate<T> extends ScrollDelegate<T> {

private final ScrollQueryFactory scrollFunction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.query.ScrollDelegate;
import org.springframework.data.jpa.support.PageableUtils;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.query.FluentQuery;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.util.Assert;
Expand All @@ -47,6 +48,7 @@
* @param <S> Domain type
* @param <R> Result type
* @author Greg Turnquist
* @author Christoph Strobl
* @since 3.0
*/
class FetchableFluentQueryBySpecification<S, R> extends FluentQuerySupport<S, R>
Expand All @@ -59,20 +61,21 @@ class FetchableFluentQueryBySpecification<S, R> extends FluentQuerySupport<S, R>
private final Function<Specification<S>, Boolean> existsOperation;
private final EntityManager entityManager;

public FetchableFluentQueryBySpecification(Specification<S> spec, Class<S> entityType,
Function<Sort, TypedQuery<S>> finder, SpecificationScrollDelegate<S> scrollDelegate,
Function<Specification<S>, Long> countOperation, Function<Specification<S>, Boolean> existsOperation,
EntityManager entityManager) {
FetchableFluentQueryBySpecification(Specification<S> spec, Class<S> entityType, Function<Sort, TypedQuery<S>> finder,
SpecificationScrollDelegate<S> scrollDelegate, Function<Specification<S>, Long> countOperation,
Function<Specification<S>, Boolean> existsOperation, EntityManager entityManager,
ProjectionFactory projectionFactory) {
this(spec, entityType, (Class<R>) entityType, Sort.unsorted(), 0, Collections.emptySet(), finder, scrollDelegate,
countOperation, existsOperation, entityManager);
countOperation, existsOperation, entityManager, projectionFactory);
}

private FetchableFluentQueryBySpecification(Specification<S> spec, Class<S> entityType, Class<R> resultType,
Sort sort, int limit, Collection<String> properties, Function<Sort, TypedQuery<S>> finder,
SpecificationScrollDelegate<S> scrollDelegate, Function<Specification<S>, Long> countOperation,
Function<Specification<S>, Boolean> existsOperation, EntityManager entityManager) {
Function<Specification<S>, Boolean> existsOperation, EntityManager entityManager,
ProjectionFactory projectionFactory) {

super(resultType, sort, limit, properties, entityType);
super(resultType, sort, limit, properties, entityType, projectionFactory);
this.spec = spec;
this.finder = finder;
this.scroll = scrollDelegate;
Expand All @@ -87,7 +90,7 @@ public FetchableFluentQuery<R> sortBy(Sort sort) {
Assert.notNull(sort, "Sort must not be null");

return new FetchableFluentQueryBySpecification<>(spec, entityType, resultType, this.sort.and(sort), limit,
properties, finder, scroll, countOperation, existsOperation, entityManager);
properties, finder, scroll, countOperation, existsOperation, entityManager, projectionFactory);
}

@Override
Expand All @@ -96,7 +99,7 @@ public FetchableFluentQuery<R> limit(int limit) {
Assert.isTrue(limit >= 0, "Limit must not be negative");

return new FetchableFluentQueryBySpecification<>(spec, entityType, resultType, this.sort.and(sort), limit,
properties, finder, scroll, countOperation, existsOperation, entityManager);
properties, finder, scroll, countOperation, existsOperation, entityManager, projectionFactory);
}

@Override
Expand All @@ -108,14 +111,14 @@ public <NR> FetchableFluentQuery<NR> as(Class<NR> resultType) {
}

return new FetchableFluentQueryBySpecification<>(spec, entityType, resultType, sort, limit, properties, finder,
scroll, countOperation, existsOperation, entityManager);
scroll, countOperation, existsOperation, entityManager, projectionFactory);
}

@Override
public FetchableFluentQuery<R> project(Collection<String> properties) {

return new FetchableFluentQueryBySpecification<>(spec, entityType, resultType, sort, limit, properties, finder,
scroll, countOperation, existsOperation, entityManager);
scroll, countOperation, existsOperation, entityManager, projectionFactory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Sort;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.lang.Nullable;

/**
Expand All @@ -36,6 +36,7 @@
* @author Greg Turnquist
* @author Jens Schauder
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.6
*/
abstract class FluentQuerySupport<S, R> {
Expand All @@ -45,11 +46,10 @@ abstract class FluentQuerySupport<S, R> {
protected final int limit;
protected final Set<String> properties;
protected final Class<S> entityType;

private final SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
protected final ProjectionFactory projectionFactory;

FluentQuerySupport(Class<R> resultType, Sort sort, int limit, @Nullable Collection<String> properties,
Class<S> entityType) {
Class<S> entityType, ProjectionFactory projectionFactory) {

this.resultType = resultType;
this.sort = sort;
Expand All @@ -62,6 +62,7 @@ abstract class FluentQuerySupport<S, R> {
}

this.entityType = entityType;
this.projectionFactory = projectionFactory;
}

final Collection<String> mergeProperties(Collection<String> additionalProperties) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.support;

import org.springframework.data.jpa.repository.query.EscapeCharacter;
import org.springframework.data.projection.ProjectionFactory;

/**
* Interface to be implemented by classes that want to be aware of their configuration in a JPA repository context.
*
* @author Mark Paluch
* @since 3.2.6
*/
public interface JpaRepositoryConfigurationAware {

/**
* Configures the {@link EscapeCharacter} to be used with the repository.
*
* @param escapeCharacter must not be {@literal null}.
*/
default void setEscapeCharacter(EscapeCharacter escapeCharacter) {

}

/**
* Configures the {@link ProjectionFactory} to be used with the repository.
*
* @param projectionFactory must not be {@literal null}.
*/
default void setProjectionFactory(ProjectionFactory projectionFactory) {

}

/**
* Configures the {@link CrudMethodMetadata} to be used with the repository.
*
* @param metadata must not be {@literal null}.
*/
default void setRepositoryMethodMetadata(CrudMethodMetadata metadata) {

}
}
Loading