-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FluentQuery for Querydsl and Query by Example.
Add support for both QueryByExampleExecutor and QuerydslPredicateExecutor. This manifests in SimpleJpaRepository and QuerydslJpaPredicateExecutor, resulting in various test cases proving support by both examples and Querydsl predicates. Closes #2294.
- Loading branch information
Showing
13 changed files
with
930 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/org/springframework/data/jpa/repository/query/DtoInstantiatingConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.springframework.data.jpa.repository.query; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.mapping.PersistentEntity; | ||
import org.springframework.data.mapping.PersistentProperty; | ||
import org.springframework.data.mapping.PersistentPropertyAccessor; | ||
import org.springframework.data.mapping.PreferredConstructor; | ||
import org.springframework.data.mapping.PreferredConstructor.Parameter; | ||
import org.springframework.data.mapping.SimplePropertyHandler; | ||
import org.springframework.data.mapping.context.MappingContext; | ||
import org.springframework.data.mapping.model.EntityInstantiator; | ||
import org.springframework.data.mapping.model.EntityInstantiators; | ||
import org.springframework.data.mapping.model.ParameterValueProvider; | ||
import org.springframework.util.Assert; | ||
|
||
public class DtoInstantiatingConverter implements Converter<Object, Object> { | ||
|
||
private final Class<?> targetType; | ||
private final MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context; | ||
private final EntityInstantiator instantiator; | ||
|
||
public DtoInstantiatingConverter(Class<?> dtoType, | ||
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context, | ||
EntityInstantiators entityInstantiators) { | ||
|
||
Assert.notNull(dtoType, "DTO type must not be null!"); | ||
Assert.notNull(context, "MappingContext must not be null!"); | ||
Assert.notNull(entityInstantiators, "EntityInstantiators must not be null!"); | ||
|
||
this.targetType = dtoType; | ||
this.context = context; | ||
System.out.println(this.context.getManagedTypes()); | ||
PersistentEntity<?, ? extends PersistentProperty<?>> requiredPersistentEntity = context | ||
.getRequiredPersistentEntity(dtoType); | ||
this.instantiator = entityInstantiators.getInstantiatorFor(requiredPersistentEntity); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public Object convert(Object source) { | ||
|
||
if (targetType.isInterface()) { | ||
return source; | ||
} | ||
|
||
PersistentEntity<?, ?> sourceEntity = context.getRequiredPersistentEntity(source.getClass()); | ||
PersistentPropertyAccessor<?> sourceAccessor = sourceEntity.getPropertyAccessor(source); | ||
PersistentEntity<?, ?> targetEntity = context.getRequiredPersistentEntity(targetType); | ||
PreferredConstructor<?, ? extends PersistentProperty<?>> constructor = targetEntity.getPersistenceConstructor(); | ||
|
||
Object dto = instantiator.createInstance(targetEntity, new ParameterValueProvider() { | ||
|
||
@Override | ||
public Object getParameterValue(Parameter parameter) { | ||
return sourceAccessor.getProperty(sourceEntity.getPersistentProperty(parameter.getName())); | ||
} | ||
}); | ||
|
||
PersistentPropertyAccessor<?> dtoAccessor = targetEntity.getPropertyAccessor(dto); | ||
|
||
targetEntity.doWithProperties((SimplePropertyHandler) property -> { | ||
|
||
if (constructor.isConstructorParameter(property)) { | ||
return; | ||
} | ||
|
||
dtoAccessor.setProperty(property, | ||
sourceAccessor.getProperty(sourceEntity.getPersistentProperty(property.getName()))); | ||
}); | ||
|
||
return dto; | ||
} | ||
} |
208 changes: 208 additions & 0 deletions
208
...ain/java/org/springframework/data/jpa/repository/query/FetchableFluentQueryByExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
/* | ||
* Copyright 2013-2021 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.query; | ||
|
||
import static org.springframework.data.jpa.repository.query.QueryUtils.*; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.TypedQuery; | ||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.CriteriaQuery; | ||
import javax.persistence.criteria.Predicate; | ||
import javax.persistence.criteria.Root; | ||
|
||
import org.springframework.dao.IncorrectResultSizeDataAccessException; | ||
import org.springframework.data.domain.Example; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder; | ||
import org.springframework.data.mapping.PersistentEntity; | ||
import org.springframework.data.mapping.PersistentProperty; | ||
import org.springframework.data.mapping.context.MappingContext; | ||
import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery; | ||
import org.springframework.data.support.PageableExecutionUtils; | ||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* Immutable implementation of {@link FetchableFluentQuery} based on Query by {@link Example}. All methods that return a | ||
* {@link FetchableFluentQuery} will return a new instance, not the original. | ||
* | ||
* @param <S> Domain type | ||
* @param <R> Result type | ||
* @author Greg Turnquist | ||
* @author Michael J. Simons | ||
* @author Mark Paluch | ||
* @since 2.6 | ||
*/ | ||
public class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<R> implements FetchableFluentQuery<R> { | ||
|
||
private Example<S> example; | ||
private Function<Sort, TypedQuery<S>> finder; | ||
private Function<Example<S>, Long> countOperation; | ||
private Function<Example<S>, Boolean> existsOperation; | ||
private EntityManager entityManager; | ||
private EscapeCharacter escapeCharacter; | ||
|
||
public FetchableFluentQueryByExample(Example<S> example, Function<Sort, TypedQuery<S>> finder, | ||
Function<Example<S>, Long> countOperation, Function<Example<S>, Boolean> existsOperation, | ||
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context, | ||
EntityManager entityManager, EscapeCharacter escapeCharacter) { | ||
this(example, (Class<R>) example.getProbeType(), Sort.unsorted(), null, finder, countOperation, existsOperation, | ||
context, entityManager, escapeCharacter); | ||
} | ||
|
||
private FetchableFluentQueryByExample(Example<S> example, Class<R> returnType, Sort sort, | ||
@Nullable Collection<String> properties, Function<Sort, TypedQuery<S>> finder, | ||
Function<Example<S>, Long> countOperation, Function<Example<S>, Boolean> existsOperation, | ||
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context, | ||
EntityManager entityManager, EscapeCharacter escapeCharacter) { | ||
|
||
super(returnType, sort, properties, context); | ||
this.example = example; | ||
this.finder = finder; | ||
this.countOperation = countOperation; | ||
this.existsOperation = existsOperation; | ||
this.entityManager = entityManager; | ||
this.escapeCharacter = escapeCharacter; | ||
} | ||
|
||
@Override | ||
public FetchableFluentQuery<R> sortBy(Sort sort) { | ||
|
||
return new FetchableFluentQueryByExample<S, R>(this.example, this.resultType, this.sort.and(sort), this.properties, | ||
this.finder, this.countOperation, this.existsOperation, this.context, this.entityManager, this.escapeCharacter); | ||
} | ||
|
||
@Override | ||
public <NR> FetchableFluentQuery<NR> as(Class<NR> resultType) { | ||
|
||
return new FetchableFluentQueryByExample<S, NR>(this.example, resultType, this.sort, this.properties, this.finder, | ||
this.countOperation, this.existsOperation, this.context, this.entityManager, this.escapeCharacter); | ||
} | ||
|
||
@Override | ||
public FetchableFluentQuery<R> project(Collection<String> properties) { | ||
|
||
return new FetchableFluentQueryByExample<>(this.example, this.resultType, this.sort, mergeProperties(properties), | ||
this.finder, this.countOperation, this.existsOperation, this.context, this.entityManager, this.escapeCharacter); | ||
} | ||
|
||
@Override | ||
public R oneValue() { | ||
|
||
List<R> all = all(); | ||
|
||
if (all.size() > 1) { | ||
throw new IncorrectResultSizeDataAccessException(1); | ||
} | ||
|
||
return all.isEmpty() ? null : all.get(0); | ||
} | ||
|
||
@Override | ||
public R firstValue() { | ||
|
||
List<R> all = all(); | ||
return all.isEmpty() ? null : all.get(0); | ||
} | ||
|
||
@Override | ||
public List<R> all() { | ||
return stream().collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public Page<R> page(Pageable pageable) { | ||
return pageable.isUnpaged() ? new PageImpl<>(all()) : readPage(pageable); | ||
} | ||
|
||
@Override | ||
public Stream<R> stream() { | ||
|
||
if (this.resultType != this.example.getProbeType()) { | ||
System.out.println("You have a projection!"); | ||
} | ||
|
||
return this.finder.apply(this.sort) // | ||
.getResultStream() // | ||
.map(getConversionFunction(this.example.getProbeType(), this.resultType)); | ||
} | ||
|
||
@Override | ||
public long count() { | ||
return this.countOperation.apply(example); | ||
} | ||
|
||
@Override | ||
public boolean exists() { | ||
return this.existsOperation.apply(example); | ||
} | ||
|
||
private Page<R> readPage(Pageable pageable) { | ||
|
||
TypedQuery<S> pagedQuery = this.finder.apply(this.sort); | ||
|
||
if (pageable.isPaged()) { | ||
pagedQuery.setFirstResult((int) pageable.getOffset()); | ||
pagedQuery.setMaxResults(pageable.getPageSize()); | ||
} | ||
|
||
List<R> paginatedResults = pagedQuery.getResultStream() // | ||
.map(getConversionFunction(this.example.getProbeType(), this.resultType)) // | ||
.collect(Collectors.toList()); | ||
|
||
return PageableExecutionUtils.getPage(paginatedResults, pageable, () -> this.countOperation.apply(this.example)); | ||
} | ||
|
||
/** | ||
* Draft version of a projection-based query using class-based DTOs. | ||
* | ||
* @param sort | ||
* @param queryType | ||
* @param example | ||
* @return | ||
*/ | ||
private TypedQuery<R> createProjectionQueryByExample(Sort sort, Class<R> queryType, Example<S> example) { | ||
|
||
CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); | ||
CriteriaQuery<R> query = builder.createQuery(queryType); | ||
|
||
Root<R> root = query.from(queryType); | ||
query.select(root); | ||
|
||
Predicate predicate = QueryByExamplePredicateBuilder.getPredicate( | ||
builder.createQuery(example.getProbeType()).from(example.getProbeType()), builder, example, escapeCharacter); | ||
|
||
if (predicate != null) { | ||
query.where(predicate); | ||
} | ||
|
||
if (sort.isSorted()) { | ||
query.orderBy(toOrders(sort, root, builder)); | ||
} | ||
|
||
return this.entityManager.createQuery(query); | ||
} | ||
|
||
} |
Oops, something went wrong.