Skip to content

Commit c95a3e9

Browse files
GH-2537 - Improve documentation.
# Conflicts: # src/test/java/org/springframework/data/neo4j/integration/issues/IssuesIT.java # src/test/java/org/springframework/data/neo4j/integration/issues/gh2323/PersonRepository.java # src/test/java/org/springframework/data/neo4j/integration/issues/gh2323/PersonService.java
1 parent 375575e commit c95a3e9

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,10 @@ All properties - and type of relationships - appear in those maps as they would
357357
have been written by SDN.
358358
Values will have the correct Cypher type and won't need further conversion.
359359

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

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

+39
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.assertThat;
1919

2020
import java.util.Arrays;
21+
import java.util.Collections;
2122
import java.util.List;
2223
import java.util.Optional;
2324
import java.util.stream.Collectors;
@@ -65,6 +66,9 @@ protected static void setupData(@Autowired BookmarkCapture bookmarkCapture) {
6566
.asString();
6667
transaction.run("unwind ['German', 'English'] as name create (n:Language {name: name}) return name")
6768
.consume();
69+
personId = transaction.run("MATCH (l:Language {name: 'German'}) CREATE (n:Person {id: randomUUID(), name: 'Helge'}) -[:HAS_MOTHER_TONGUE]-> (l) return n.id").single()
70+
.get(0)
71+
.asString();
6872
transaction.commit();
6973
bookmarkCapture.seedWith(session.lastBookmark());
7074
}
@@ -105,6 +109,20 @@ void ensureRelationshipsAreSerialized(@Autowired PersonService personService) {
105109
});
106110
}
107111

112+
@Test // "GH-2537"
113+
void ensure1To1RelationshipsAreSerialized(@Autowired PersonService personService) {
114+
115+
Optional<Person> optionalPerson = personService.updateRel3(personId);
116+
assertThat(optionalPerson).isPresent().hasValueSatisfying(person -> {
117+
118+
assertThat(person.getKnownLanguages()).hasSize(1);
119+
assertThat(person.getKnownLanguages()).first().satisfies(knows -> {
120+
assertThat(knows.getDescription()).isEqualTo("Whatever");
121+
assertThat(knows.getLanguage()).extracting(Language::getName).isEqualTo("German");
122+
});
123+
});
124+
}
125+
108126
@Repository
109127
public interface PersonRepository extends Neo4jRepository<Person, String> {
110128

@@ -126,6 +144,16 @@ public interface PersonRepository extends Neo4jRepository<Person, String> {
126144
+ "CREATE (f) - [r:KNOWS {description: rel.__properties__.description}] -> (t) "
127145
+ "RETURN f, collect(r), collect(t)")
128146
Person updateRel2(@Param("person") Person person);
147+
148+
@Query(""
149+
+ "MATCH (f:Person {id: $person.__id__}) "
150+
+ "MATCH (mt:Language {name: $person.__properties__.HAS_MOTHER_TONGUE[0].__target__.__id__}) "
151+
+ "MATCH (f)-[frl:HAS_MOTHER_TONGUE]->(mt) WITH f, frl, mt "
152+
+ "UNWIND $person.__properties__.KNOWS As rel WITH f, frl, mt, rel "
153+
+ "MATCH (t:Language {name: rel.__target__.__id__}) "
154+
+ "MERGE (f)- [r:KNOWS {description: rel.__properties__.description}] -> (t) "
155+
+ "RETURN f, frl, mt, collect(r), collect(t)")
156+
Person updateRelWith11(@Param("person") Person person);
129157
}
130158

131159
@Service
@@ -159,6 +187,17 @@ public Optional<Person> updateRel2(String id, List<String> languageNames) {
159187

160188
return original;
161189
}
190+
191+
public Optional<Person> updateRel3(String id) {
192+
Optional<Person> original = personRepository.findById(id);
193+
if (original.isPresent()) {
194+
Person person = original.get();
195+
person.setKnownLanguages(Collections.singletonList(new Knows("Whatever", new Language("German"))));
196+
return Optional.of(personRepository.updateRelWith11(person));
197+
}
198+
199+
return original;
200+
}
162201
}
163202

164203
@Configuration

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

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

0 commit comments

Comments
 (0)