Skip to content

Commit 3e6c5aa

Browse files
committed
DATAGRAPH-1411 - Dynamic relationships for custom queries.
1 parent a62d92b commit 3e6c5aa

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jEntityConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ private Optional<Object> createInstanceOfRelationships(Neo4jPersistentProperty p
449449
// find relationships in the result
450450
List<Relationship> allMatchingTypeRelationshipsInResult = StreamSupport
451451
.stream(allValues.values().spliterator(), false).filter(isList.and(containsOnlyRelationships))
452-
.flatMap(entry -> entry.asList(Value::asRelationship).stream()).filter(r -> r.type().equals(relationshipType))
452+
.flatMap(entry -> entry.asList(Value::asRelationship).stream())
453+
.filter(r -> r.type().equals(relationshipType) || relationshipDescription.isDynamic())
453454
.collect(Collectors.toList());
454455

455456
List<Node> allNodesWithMatchingLabelInResult = StreamSupport.stream(allValues.values().spliterator(), false)

src/test/java/org/springframework/data/neo4j/integration/imperative/DynamicRelationshipsIT.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
import org.springframework.data.neo4j.integration.shared.PersonWithRelatives.TypeOfRelative;
4545
import org.springframework.data.neo4j.integration.shared.Pet;
4646
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
47+
import org.springframework.data.neo4j.repository.query.Query;
4748
import org.springframework.data.repository.CrudRepository;
49+
import org.springframework.data.repository.query.Param;
4850
import org.springframework.test.util.ReflectionTestUtils;
4951
import org.springframework.transaction.annotation.EnableTransactionManagement;
5052

@@ -261,7 +263,47 @@ void shouldWriteDynamicCollectionRelationships(@Autowired PersonWithRelativesRep
261263
}
262264
}
263265

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+
}
265307

266308
@Configuration
267309
@EnableTransactionManagement

src/test/java/org/springframework/data/neo4j/integration/imperative/StringlyTypedDynamicRelationshipsIT.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import org.springframework.data.neo4j.integration.shared.PersonWithStringlyTypedRelatives;
4141
import org.springframework.data.neo4j.integration.shared.Pet;
4242
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
43+
import org.springframework.data.neo4j.repository.query.Query;
4344
import org.springframework.data.repository.CrudRepository;
45+
import org.springframework.data.repository.query.Param;
4446
import org.springframework.test.util.ReflectionTestUtils;
4547
import org.springframework.transaction.annotation.EnableTransactionManagement;
4648

@@ -255,7 +257,46 @@ void shouldWriteDynamicCollectionRelationships(@Autowired PersonWithRelativesRep
255257
}
256258
}
257259

258-
interface PersonWithRelativesRepository extends CrudRepository<PersonWithStringlyTypedRelatives, Long> {}
260+
@Test // DATAGRAPH-1411
261+
void shouldReadDynamicRelationshipsWithCustomQuery(@Autowired PersonWithRelativesRepository repository) {
262+
263+
PersonWithStringlyTypedRelatives person = repository.byCustomQuery(idOfExistingPerson);
264+
assertThat(person).isNotNull();
265+
assertThat(person.getName()).isEqualTo("A");
266+
267+
Map<String, Person> relatives = person.getRelatives();
268+
assertThat(relatives).containsOnlyKeys("HAS_WIFE", "HAS_DAUGHTER");
269+
assertThat(relatives.get("HAS_WIFE").getFirstName()).isEqualTo("B");
270+
assertThat(relatives.get("HAS_DAUGHTER").getFirstName()).isEqualTo("C");
271+
272+
Map<String, ClubRelationship> clubs = person.getClubs();
273+
assertThat(clubs).containsOnlyKeys("FOOTBALL");
274+
assertThat(clubs.get("FOOTBALL").getPlace()).isEqualTo("Brunswick");
275+
assertThat(clubs.get("FOOTBALL").getClub().getName()).isEqualTo("BTSV");
276+
}
277+
278+
@Test // DATAGRAPH-1411
279+
void shouldReadDynamicCollectionRelationshipsWithCustomQuery(@Autowired PersonWithRelativesRepository repository) {
280+
281+
PersonWithStringlyTypedRelatives person = repository.byCustomQuery(idOfExistingPerson);
282+
assertThat(person).isNotNull();
283+
assertThat(person.getName()).isEqualTo("A");
284+
285+
Map<String, List<Pet>> pets = person.getPets();
286+
assertThat(pets).containsOnlyKeys("CATS", "DOGS");
287+
assertThat(pets.get("CATS")).extracting(Pet::getName).containsExactlyInAnyOrder("Tom", "Garfield");
288+
assertThat(pets.get("DOGS")).extracting(Pet::getName).containsExactlyInAnyOrder("Benji", "Lassie");
289+
290+
Map<String, List<HobbyRelationship>> hobbies = person.getHobbies();
291+
assertThat(hobbies.get("ACTIVE")).extracting(HobbyRelationship::getPerformance).containsExactly("average");
292+
assertThat(hobbies.get("ACTIVE")).extracting(HobbyRelationship::getHobby).extracting(Hobby::getName).containsExactly("Biking");
293+
}
294+
295+
interface PersonWithRelativesRepository extends CrudRepository<PersonWithStringlyTypedRelatives, Long> {
296+
297+
@Query("MATCH (p:PersonWithStringlyTypedRelatives)-[r] -> (o) WHERE id(p) = $personId return p, collect(r), collect(o)")
298+
PersonWithStringlyTypedRelatives byCustomQuery(@Param("personId") Long personId);
299+
}
259300

260301
@Configuration
261302
@EnableTransactionManagement

0 commit comments

Comments
 (0)