Use by-id lookup for queries referring to identifier values.

Closes: #2851
Original Pull Request: #2854
This commit is contained in:
Mark Paluch
2024-02-20 14:27:25 +01:00
committed by Christoph Strobl
parent 32ace71175
commit 3c44521026
2 changed files with 117 additions and 6 deletions

View File

@@ -27,6 +27,7 @@ import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Reference;
@@ -101,6 +102,57 @@ public abstract class RedisRepositoryIntegrationTestBase {
assertThat(repo.findByLastname("al'thor")).contains(rand);
}
@Test // GH-2851
void shouldReturnSingleEntityByIdViaQueryMethod() {
Person rand = new Person();
rand.firstname = "rand";
rand.lastname = "al'thor";
Person egwene = new Person();
egwene.firstname = "egwene";
repo.saveAll(Arrays.asList(rand, egwene));
assertThat(repo.findEntityById(rand.getId())).isEqualTo(rand);
assertThat(repo.findEntityById(egwene.getId())).isEqualTo(egwene);
}
@Test // GH-2851
void shouldProjectSingleResult() {
Person rand = new Person();
rand.firstname = "rand";
rand.lastname = "al'thor";
Person egwene = new Person();
egwene.firstname = "egwene";
repo.saveAll(Arrays.asList(rand, egwene));
PersonProjection projectionById = repo.findProjectionById(rand.getId());
assertThat(projectionById).isNotNull();
assertThat(projectionById.getFirstname()).isEqualTo(rand.firstname);
}
@Test // GH-2851
void shouldProjectCollection() {
Person rand = new Person();
rand.firstname = "rand";
rand.lastname = "al'thor";
Person egwene = new Person();
egwene.firstname = "egwene";
repo.saveAll(Arrays.asList(rand, egwene));
List<PersonProjection> projectionById = repo.findProjectionBy();
assertThat(projectionById).hasSize(2) //
.extracting(PersonProjection::getFirstname) //
.contains(rand.getFirstname(), egwene.getFirstname());
}
@Test // DATAREDIS-425
void simpleFindByMultipleProperties() {
@@ -570,10 +622,20 @@ public abstract class RedisRepositoryIntegrationTestBase {
Slice<Person> findByHometownLocationNear(Point point, Distance distance, Pageable pageable);
Person findEntityById(String id);
PersonProjection findProjectionById(String id);
List<PersonProjection> findProjectionBy();
@Override
<S extends Person> List<S> findAll(Example<S> example);
}
public interface PersonProjection {
String getFirstname();
}
public interface CityRepository extends CrudRepository<City, String> {
List<City> findByLocationNear(Point point, Distance distance);