Skip to content

Add support for fluent Querydsl and Query by Example query definition #3788

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
4 changes: 2 additions & 2 deletions 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-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-3757-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand All @@ -26,7 +26,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>2.6.0-SNAPSHOT</springdata.commons>
<springdata.commons>2.6.0-2228-SNAPSHOT</springdata.commons>
<mongo>4.3.1</mongo>
<mongo.reactivestreams>${mongo}</mongo.reactivestreams>
<jmh.version>1.19</jmh.version>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-3757-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-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-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-3757-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,6 @@ public <T> T save(T objectToSave, String collectionName) {
return source.isVersionedEntity() //
? doSaveVersioned(source, collectionName) //
: (T) doSave(collectionName, objectToSave, this.mongoConverter);

}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ public boolean isSorted() {
* @throws IllegalArgumentException when {@code fieldsObject} is {@literal null}.
* @since 1.6
*/
protected void setFieldsObject(Document fieldsObject) {
public void setFieldsObject(Document fieldsObject) {

Assert.notNull(sortObject, "Field document must not be null");
Assert.notNull(fieldsObject, "Field document must not be null");

this.fieldsObject = fieldsObject;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 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.mongodb.repository.support;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.FluentQuery;
import org.springframework.util.Assert;

/**
* Support class for {@link org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery} implementations.
*
* @author Mark Paluch
* @since 3.3
*/
abstract class FetchableFluentQuerySupport<P, T> implements FluentQuery.FetchableFluentQuery<T> {

private final P predicate;
private final Sort sort;
private final Class<T> resultType;
private final List<String> fieldsToInclude;

FetchableFluentQuerySupport(P predicate, Sort sort, Class<T> resultType, List<String> fieldsToInclude) {
this.predicate = predicate;
this.sort = sort;
this.resultType = resultType;
this.fieldsToInclude = fieldsToInclude;
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#sortBy(org.springframework.data.domain.Sort)
*/
@Override
public FluentQuery.FetchableFluentQuery<T> sortBy(Sort sort) {

Assert.notNull(sort, "Sort must not be null!");

return create(predicate, sort, resultType, fieldsToInclude);
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#as(java.lang.Class)
*/
@Override
public <R> FluentQuery.FetchableFluentQuery<R> as(Class<R> projection) {

Assert.notNull(projection, "Projection target type must not be null!");

return create(predicate, sort, projection, fieldsToInclude);
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#project(java.util.Collection)
*/
@Override
public FluentQuery.FetchableFluentQuery<T> project(Collection<String> properties) {

Assert.notNull(properties, "Projection properties must not be null!");

return create(predicate, sort, resultType, new ArrayList<>(properties));
}

protected abstract <R> FetchableFluentQuerySupport<P, R> create(P predicate, Sort sort, Class<R> resultType,
List<String> fieldsToInclude);

P getPredicate() {
return predicate;
}

Sort getSort() {
return sort;
}

Class<T> getResultType() {
return resultType;
}

List<String> getFieldsToInclude() {
return fieldsToInclude;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
*/
package org.springframework.data.mongodb.repository.support;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

import org.bson.Document;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.querydsl.EntityPathResolver;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.data.repository.query.FluentQuery;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -184,6 +190,21 @@ public boolean exists(Predicate predicate) {
return createQueryFor(predicate).fetchCount() > 0;
}

/*
* (non-Javadoc)
* @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findBy(com.querydsl.core.types.Predicate, java.util.function.Function)
*/
@Override
@SuppressWarnings("unchecked")
public <S extends T, R> R findBy(Predicate predicate,
Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction) {

Assert.notNull(predicate, "Predicate must not be null!");
Assert.notNull(queryFunction, "Query function must not be null!");

return queryFunction.apply(new FluentQuerydsl<>(predicate, (Class<S>) typeInformation().getJavaType()));
}

/**
* Creates a {@link SpringDataMongodbQuery} for the given {@link Predicate}.
*
Expand Down Expand Up @@ -232,4 +253,113 @@ private SpringDataMongodbQuery<T> applySorting(SpringDataMongodbQuery<T> query,
toOrderSpecifiers(sort).forEach(query::orderBy);
return query;
}

/**
* {@link org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery} using Querydsl
* {@link Predicate}.
*
* @author Mark Paluch
* @since 3.3
*/
class FluentQuerydsl<T> extends FetchableFluentQuerySupport<Predicate, T> {

FluentQuerydsl(Predicate predicate, Class<T> resultType) {
this(predicate, Sort.unsorted(), resultType, Collections.emptyList());
}

FluentQuerydsl(Predicate predicate, Sort sort, Class<T> resultType, List<String> fieldsToInclude) {
super(predicate, sort, resultType, fieldsToInclude);
}

@Override
protected <R> FluentQuerydsl<R> create(Predicate predicate, Sort sort, Class<R> resultType,
List<String> fieldsToInclude) {
return new FluentQuerydsl<>(predicate, sort, resultType, fieldsToInclude);
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#oneValue()
*/
@Override
public T oneValue() {
return createQuery().fetchOne();
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#firstValue()
*/
@Override
public T firstValue() {
return createQuery().fetchFirst();
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#all()
*/
@Override
public List<T> all() {
return createQuery().fetch();
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#page(org.springframework.data.domain.Pageable)
*/
@Override
public Page<T> page(Pageable pageable) {

Assert.notNull(pageable, "Pageable must not be null!");

return createQuery().fetchPage(pageable);
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#stream()
*/
@Override
public Stream<T> stream() {
return createQuery().stream();
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#count()
*/
@Override
public long count() {
return createQuery().fetchCount();
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#exists()
*/
@Override
public boolean exists() {
return count() > 0;
}

private SpringDataMongodbQuery<T> createQuery() {
return new SpringDataMongodbQuery<>(mongoOperations, typeInformation().getJavaType(), getResultType(),
mongoOperations.getCollectionName(typeInformation().getJavaType()), this::customize).where(getPredicate());
}

private void customize(BasicQuery query) {

List<String> fieldsToInclude = getFieldsToInclude();
if (!fieldsToInclude.isEmpty()) {
Document fields = new Document();
fieldsToInclude.forEach(field -> fields.put(field, 1));
query.setFieldsObject(fields);
}

if (getSort().isSorted()) {
query.with(getSort());
}
}
}
}
Loading