DATAKV-179 - Adapt to API changes in QuerydslPredicateExecutor.findOne(…).
Related ticket: DATACMNS-1059.
This commit is contained in:
@@ -15,10 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.keyvalue.repository.support;
|
||||
|
||||
import static org.springframework.data.keyvalue.repository.support.KeyValueQuerydslUtils.toOrderSpecifier;
|
||||
import static org.springframework.data.keyvalue.repository.support.KeyValueQuerydslUtils.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -34,6 +36,7 @@ import org.springframework.util.ObjectUtils;
|
||||
|
||||
import com.querydsl.collections.AbstractCollQuery;
|
||||
import com.querydsl.collections.CollQuery;
|
||||
import com.querydsl.core.NonUniqueResultException;
|
||||
import com.querydsl.core.types.EntityPath;
|
||||
import com.querydsl.core.types.OrderSpecifier;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
@@ -91,8 +94,13 @@ public class QuerydslKeyValueRepository<T, ID extends Serializable> extends Simp
|
||||
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findOne(com.mysema.query.types.Predicate)
|
||||
*/
|
||||
@Override
|
||||
public T findOne(Predicate predicate) {
|
||||
return prepareQuery(predicate).fetchOne();
|
||||
public Optional<T> findOne(Predicate predicate) {
|
||||
|
||||
try {
|
||||
return Optional.ofNullable(prepareQuery(predicate).fetchOne());
|
||||
} catch (NonUniqueResultException o_O) {
|
||||
throw new IncorrectResultSizeDataAccessException("Expected one or no result but found more than one!", 1, o_O);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -15,12 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -49,8 +53,8 @@ public class QuerydslKeyValueRepositoryUnitTests extends AbstractRepositoryUnitT
|
||||
|
||||
repository.saveAll(LENNISTERS);
|
||||
|
||||
Person result = repository.findOne(QPerson.person.firstname.eq(CERSEI.getFirstname()));
|
||||
assertThat(result, is(CERSEI));
|
||||
Optional<Person> result = repository.findOne(QPerson.person.firstname.eq(CERSEI.getFirstname()));
|
||||
assertThat(result).hasValue(CERSEI);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-525
|
||||
@@ -154,6 +158,15 @@ public class QuerydslKeyValueRepositoryUnitTests extends AbstractRepositoryUnitT
|
||||
assertThat(users, hasItems(CERSEI, JAIME, TYRION));
|
||||
}
|
||||
|
||||
@Test // DATAKV-179
|
||||
public void throwsExceptionIfMoreThanOneResultIsFound() {
|
||||
|
||||
repository.saveAll(LENNISTERS);
|
||||
|
||||
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class) //
|
||||
.isThrownBy(() -> repository.findOne(person.firstname.contains("e")));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.map.SimpleKeyValueRepositoryUnitTests#getRepository(org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory)
|
||||
|
||||
Reference in New Issue
Block a user