19
19
20
20
import java .util .Arrays ;
21
21
import java .util .List ;
22
+ import java .util .Optional ;
22
23
import java .util .stream .Collectors ;
23
24
24
25
import org .junit .jupiter .api .BeforeAll ;
26
+ import org .junit .jupiter .api .BeforeEach ;
25
27
import org .junit .jupiter .api .Test ;
26
28
import org .neo4j .driver .Driver ;
27
29
import org .neo4j .driver .Session ;
@@ -68,6 +70,17 @@ protected static void setupData(@Autowired BookmarkCapture bookmarkCapture) {
68
70
}
69
71
}
70
72
73
+ @ BeforeEach
74
+ protected void removeRelationships (@ Autowired BookmarkCapture bookmarkCapture ) {
75
+ try (Session session = neo4jConnectionSupport .getDriver ().session (bookmarkCapture .createSessionConfig ());
76
+ Transaction transaction = session .beginTransaction ();
77
+ ) {
78
+ transaction .run ("MATCH ()- [r:KNOWS]-() delete r" ).consume ();
79
+ transaction .commit ();
80
+ bookmarkCapture .seedWith (session .lastBookmark ());
81
+ }
82
+ }
83
+
71
84
@ Test // GH-2323
72
85
void listOfRelationshipPropertiesShouldBeUnwindable (@ Autowired PersonService personService ) {
73
86
Person person = personService .updateRel (personId , Arrays .asList ("German" ));
@@ -79,14 +92,40 @@ void listOfRelationshipPropertiesShouldBeUnwindable(@Autowired PersonService per
79
92
});
80
93
}
81
94
95
+ @ Test // GH-2537
96
+ void ensureRelationshipsAreSerialized (@ Autowired PersonService personService ) {
97
+
98
+ Optional <Person > optionalPerson = personService .updateRel2 (personId , Arrays .asList ("German" ));
99
+ assertThat (optionalPerson ).isPresent ().hasValueSatisfying (person -> {
100
+ assertThat (person .getKnownLanguages ()).hasSize (1 );
101
+ assertThat (person .getKnownLanguages ()).first ().satisfies (knows -> {
102
+ assertThat (knows .getDescription ()).isEqualTo ("Some description" );
103
+ assertThat (knows .getLanguage ()).extracting (Language ::getName ).isEqualTo ("German" );
104
+ });
105
+ });
106
+ }
107
+
82
108
@ Repository
83
109
public interface PersonRepository extends Neo4jRepository <Person , String > {
84
110
85
- @ Query ("UNWIND $relations As rel WITH rel " +
86
- "CREATE (f:Person {id: $from}) - [r:KNOWS {description: rel.__properties__.description}] -> (t:Language {name: rel.__properties__.__target__.__id__}) "
87
- +
88
- "RETURN f, collect(r), collect(t)" )
111
+ // Using separate id and than relationships on top level
112
+ @ Query (""
113
+ + "UNWIND $relations As rel WITH rel "
114
+ + "MATCH (f:Person {id: $from}) "
115
+ + "MATCH (t:Language {name: rel.__target__.__id__}) "
116
+ + "CREATE (f)- [r:KNOWS {description: rel.__properties__.description}] -> (t) "
117
+ + "RETURN f, collect(r), collect(t)"
118
+ )
89
119
Person updateRel (@ Param ("from" ) String from , @ Param ("relations" ) List <Knows > relations );
120
+
121
+ // Using the whole person object
122
+ @ Query (""
123
+ + "UNWIND $person.__properties__.KNOWS As rel WITH rel "
124
+ + "MATCH (f:Person {id: $person.__id__}) "
125
+ + "MATCH (t:Language {name: rel.__target__.__id__}) "
126
+ + "CREATE (f) - [r:KNOWS {description: rel.__properties__.description}] -> (t) "
127
+ + "RETURN f, collect(r), collect(t)" )
128
+ Person updateRel2 (@ Param ("person" ) Person person );
90
129
}
91
130
92
131
@ Service
@@ -105,6 +144,21 @@ public Person updateRel(String from, List<String> languageNames) {
105
144
.collect (Collectors .toList ());
106
145
return personRepository .updateRel (from , knownLanguages );
107
146
}
147
+
148
+ public Optional <Person > updateRel2 (String id , List <String > languageNames ) {
149
+
150
+ Optional <Person > original = personRepository .findById (id );
151
+ if (original .isPresent ()) {
152
+ Person person = original .get ();
153
+ List <Knows > knownLanguages = languageNames .stream ().map (Language ::new )
154
+ .map (language -> new Knows ("Some description" , language ))
155
+ .collect (Collectors .toList ());
156
+ person .setKnownLanguages (knownLanguages );
157
+ return Optional .of (personRepository .updateRel2 (person ));
158
+ }
159
+
160
+ return original ;
161
+ }
108
162
}
109
163
110
164
@ Configuration
0 commit comments