|
44 | 44 | import org.springframework.data.neo4j.integration.shared.PersonWithRelatives.TypeOfRelative;
|
45 | 45 | import org.springframework.data.neo4j.integration.shared.Pet;
|
46 | 46 | import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
|
| 47 | +import org.springframework.data.neo4j.repository.query.Query; |
47 | 48 | import org.springframework.data.repository.CrudRepository;
|
| 49 | +import org.springframework.data.repository.query.Param; |
48 | 50 | import org.springframework.test.util.ReflectionTestUtils;
|
49 | 51 | import org.springframework.transaction.annotation.EnableTransactionManagement;
|
50 | 52 |
|
@@ -261,7 +263,47 @@ void shouldWriteDynamicCollectionRelationships(@Autowired PersonWithRelativesRep
|
261 | 263 | }
|
262 | 264 | }
|
263 | 265 |
|
264 |
| - interface PersonWithRelativesRepository extends CrudRepository<PersonWithRelatives, Long> {} |
| 266 | + @Test // DATAGRAPH-1411 |
| 267 | + void shouldReadDynamicRelationshipsWithCustomQuery(@Autowired PersonWithRelativesRepository repository) { |
| 268 | + |
| 269 | + PersonWithRelatives person = repository.byCustomQuery(idOfExistingPerson); |
| 270 | + assertThat(person).isNotNull(); |
| 271 | + assertThat(person.getName()).isEqualTo("A"); |
| 272 | + |
| 273 | + Map<TypeOfRelative, Person> relatives = person.getRelatives(); |
| 274 | + assertThat(relatives).containsOnlyKeys(TypeOfRelative.HAS_WIFE, TypeOfRelative.HAS_DAUGHTER); |
| 275 | + assertThat(relatives.get(TypeOfRelative.HAS_WIFE).getFirstName()).isEqualTo("B"); |
| 276 | + assertThat(relatives.get(TypeOfRelative.HAS_DAUGHTER).getFirstName()).isEqualTo("C"); |
| 277 | + |
| 278 | + Map<TypeOfClub, ClubRelationship> clubs = person.getClubs(); |
| 279 | + assertThat(clubs).containsOnlyKeys(TypeOfClub.FOOTBALL); |
| 280 | + assertThat(clubs.get(TypeOfClub.FOOTBALL).getPlace()).isEqualTo("Brunswick"); |
| 281 | + assertThat(clubs.get(TypeOfClub.FOOTBALL).getClub().getName()).isEqualTo("BTSV"); |
| 282 | + } |
| 283 | + |
| 284 | + @Test // DATAGRAPH-1411 |
| 285 | + void shouldReadDynamicCollectionRelationshipsWithCustomQuery(@Autowired PersonWithRelativesRepository repository) { |
| 286 | + |
| 287 | + PersonWithRelatives person = repository.byCustomQuery(idOfExistingPerson); |
| 288 | + assertThat(person).isNotNull(); |
| 289 | + assertThat(person.getName()).isEqualTo("A"); |
| 290 | + |
| 291 | + Map<TypeOfPet, List<Pet>> pets = person.getPets(); |
| 292 | + assertThat(pets).containsOnlyKeys(TypeOfPet.CATS, TypeOfPet.DOGS); |
| 293 | + assertThat(pets.get(TypeOfPet.CATS)).extracting(Pet::getName).containsExactlyInAnyOrder("Tom", "Garfield"); |
| 294 | + assertThat(pets.get(TypeOfPet.DOGS)).extracting(Pet::getName).containsExactlyInAnyOrder("Benji", "Lassie"); |
| 295 | + |
| 296 | + Map<TypeOfHobby, List<HobbyRelationship>> hobbies = person.getHobbies(); |
| 297 | + assertThat(hobbies.get(TypeOfHobby.ACTIVE)).extracting(HobbyRelationship::getPerformance).containsExactly("average"); |
| 298 | + assertThat(hobbies.get(TypeOfHobby.ACTIVE)).extracting(HobbyRelationship::getHobby).extracting(Hobby::getName).containsExactly("Biking"); |
| 299 | + } |
| 300 | + |
| 301 | + interface PersonWithRelativesRepository extends CrudRepository<PersonWithRelatives, Long> { |
| 302 | + |
| 303 | + @Query("MATCH (p:PersonWithRelatives)-[r] -> (o) WHERE id(p) = $personId return p, collect(r), collect(o)") |
| 304 | + PersonWithRelatives byCustomQuery(@Param("personId") Long personId); |
| 305 | + |
| 306 | + } |
265 | 307 |
|
266 | 308 | @Configuration
|
267 | 309 | @EnableTransactionManagement
|
|
0 commit comments