Skip to content

Commit ba0dc27

Browse files
GH-2537 - Improve documentation.
1 parent d6e9ebe commit ba0dc27

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

src/main/asciidoc/appendix/custom-queries.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,10 @@ All properties - and type of relationships - appear in those maps as they would
359359
have been written by SDN.
360360
Values will have the correct Cypher type and won't need further conversion.
361361

362-
All relationships are lists of maps. Dynamic relationships will be resolved accordingly.
362+
TIP: All relationships are lists of maps. Dynamic relationships will be resolved accordingly.
363+
One-to-one relationships will also be serialized as singleton lists. So to access a one-to-one mapping
364+
between people, you would write this das `$person.\\__properties__.BEST_FRIEND[0].\\__target__.\\__id__`.
365+
363366
If an entity has a relationship with the same type to different types of others nodes, they will all appear in the same list.
364367
If you need such a mapping and also have the need to work with those custom parameters, you have to unroll it accordingly.
365368
One way to do this are correlated subqueries (Neo4j 4.1+ required).

src/test/java/org/springframework/data/neo4j/integration/issues/IssuesIT.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,15 @@ private static void setupGH2210(QueryRunner queryRunner) {
186186
}
187187

188188
private static void setupGH2323(QueryRunner queryRunner) {
189-
personId = queryRunner.run("CREATE (n:Person {id: randomUUID(), name: 'Helge'}) return n.id").single()
190-
.get(0)
191-
.asString();
192189
queryRunner.run("unwind ['German', 'English'] as name create (n:Language {name: name}) return name")
193190
.consume();
191+
personId = queryRunner.run("""
192+
MATCH (l:Language {name: 'German'})
193+
CREATE (n:Person {id: randomUUID(), name: 'Helge'}) -[:HAS_MOTHER_TONGUE]-> (l)
194+
return n.id"""
195+
).single()
196+
.get(0)
197+
.asString();
194198
}
195199

196200
private static void setupGH2459(QueryRunner queryRunner) {
@@ -439,6 +443,21 @@ void ensureRelationshipsAreSerialized(@Autowired PersonService personService) {
439443
});
440444
}
441445

446+
@Test
447+
@Tag("GH-2537")
448+
void ensure1To1RelationshipsAreSerialized(@Autowired PersonService personService) {
449+
450+
Optional<Person> optionalPerson = personService.updateRel3(personId);
451+
assertThat(optionalPerson).isPresent().hasValueSatisfying(person -> {
452+
453+
assertThat(person.getKnownLanguages()).hasSize(1);
454+
assertThat(person.getKnownLanguages()).first().satisfies(knows -> {
455+
assertThat(knows.getDescription()).isEqualTo("Whatever");
456+
assertThat(knows.getLanguage()).extracting(Language::getName).isEqualTo("German");
457+
});
458+
});
459+
}
460+
442461
@Test
443462
@Tag("GH-2326")
444463
void saveShouldAddAllLabels(@Autowired AnimalRepository animalRepository,

src/test/java/org/springframework/data/neo4j/integration/issues/gh2323/Person.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class Person {
3737
@Relationship("KNOWS")
3838
private List<Knows> knownLanguages = new ArrayList<>();
3939

40+
@Relationship("HAS_MOTHER_TONGUE")
41+
private Knows motherTongue;
42+
4043
public Person(String name) {
4144
this.name = name;
4245
}

src/test/java/org/springframework/data/neo4j/integration/issues/gh2323/PersonRepository.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@ RETURN f, collect(r), collect(t)
4949
"""
5050
)
5151
Person updateRel2(@Param("person") Person person);
52+
53+
@Query("""
54+
MATCH (f:Person {id: $person.__id__})
55+
MATCH (mt:Language {name: $person.__properties__.HAS_MOTHER_TONGUE[0].__target__.__id__})
56+
MATCH (f)-[frl:HAS_MOTHER_TONGUE]->(mt) WITH f, frl, mt
57+
UNWIND $person.__properties__.KNOWS As rel WITH f, frl, mt, rel
58+
MATCH (t:Language {name: rel.__target__.__id__})
59+
MERGE (f)- [r:KNOWS {description: rel.__properties__.description}] -> (t)
60+
RETURN f, frl, mt, collect(r), collect(t)
61+
""")
62+
Person updateRelWith11(@Param("person") Person person);
5263
}

src/test/java/org/springframework/data/neo4j/integration/issues/gh2323/PersonService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,15 @@ public Optional<Person> updateRel2(String id, List<String> languageNames) {
5555

5656
return original;
5757
}
58+
59+
public Optional<Person> updateRel3(String id) {
60+
Optional<Person> original = personRepository.findById(id);
61+
if (original.isPresent()) {
62+
Person person = original.get();
63+
person.setKnownLanguages(List.of(new Knows("Whatever", new Language("German"))));
64+
return Optional.of(personRepository.updateRelWith11(person));
65+
}
66+
67+
return original;
68+
}
5869
}

0 commit comments

Comments
 (0)