DATAJPA-1148 - SimpleJpaRepository.existsById(…) now evaluates findById(…) results properly.

SimpleJpaRepository.existsById(…) now properly checks the Optional instances returned for code paths falling back to findById(…).
This commit is contained in:
Oliver Gierke
2017-07-20 10:44:18 +02:00
parent 6bedbabc51
commit 281260d68d
2 changed files with 5 additions and 6 deletions

View File

@@ -262,7 +262,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepository<T, ID>, JpaSpec
Assert.notNull(id, ID_MUST_NOT_BE_NULL);
if (entityInformation.getIdAttribute() == null) {
return findById(id) != null;
return findById(id).isPresent();
}
String placeholder = provider.getCountQueryPlaceholder();
@@ -287,7 +287,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepository<T, ID>, JpaSpec
if (complexIdParameterValueDiscovered) {
// fall-back to findById(id) which does the proper mapping for the parameter.
return findById(id) != null;
return findById(id).isPresent();
}
query.setParameter(idAttributeName, idAttributeValue);
@@ -455,8 +455,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepository<T, ID>, JpaSpec
Class<S> probeType = example.getProbeType();
TypedQuery<S> query = getQuery(new ExampleSpecification<>(example), probeType, pageable);
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList())
: readPage(query, probeType, pageable, spec);
return isUnpaged(pageable) ? new PageImpl<>(query.getResultList()) : readPage(query, probeType, pageable, spec);
}
/*

View File

@@ -25,7 +25,6 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Page;
@@ -210,7 +209,7 @@ public class RepositoryWithCompositeKeyTests {
assertThat(result.get(1), is(emp1));
}
@Test // DATAJPA-527
@Test // DATAJPA-527, DATAJPA-1148
public void testExistsWithIdClass() {
IdClassExampleDepartment dep = new IdClassExampleDepartment();
@@ -227,6 +226,7 @@ public class RepositoryWithCompositeKeyTests {
key.setEmpId(emp.getEmpId());
assertThat(employeeRepositoryWithIdClass.existsById(key), is(true));
assertThat(employeeRepositoryWithIdClass.existsById(new IdClassExampleEmployeePK(0L, 0L)), is(false));
}
@Test // DATAJPA-527