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 ;
30
32
import org .springframework .context .annotation .Bean ;
31
33
import org .springframework .context .annotation .ComponentScan ;
32
34
import org .springframework .context .annotation .Configuration ;
33
- import org .springframework .data .neo4j .test .Neo4jImperativeTestConfiguration ;
34
35
import org .springframework .data .neo4j .repository .Neo4jRepository ;
35
36
import org .springframework .data .neo4j .repository .config .EnableNeo4jRepositories ;
36
37
import org .springframework .data .neo4j .repository .query .Query ;
37
38
import org .springframework .data .neo4j .test .BookmarkCapture ;
38
39
import org .springframework .data .neo4j .test .Neo4jExtension ;
40
+ import org .springframework .data .neo4j .test .Neo4jImperativeTestConfiguration ;
39
41
import org .springframework .data .neo4j .test .Neo4jIntegrationTest ;
40
42
import org .springframework .data .repository .query .Param ;
41
43
import org .springframework .stereotype .Repository ;
@@ -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,43 @@ 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
+ """
119
+ )
89
120
Person updateRel (@ Param ("from" ) String from , @ Param ("relations" ) List <Knows > relations );
121
+
122
+ // Using the whole person object
123
+ @ Query ("""
124
+ UNWIND $person.__properties__.KNOWS As rel WITH rel
125
+ MATCH (f:Person {id: $person.__id__})
126
+ MATCH (t:Language {name: rel.__target__.__id__})
127
+ CREATE (f) - [r:KNOWS {description: rel.__properties__.description}] -> (t)
128
+ RETURN f, collect(r), collect(t)
129
+ """
130
+ )
131
+ Person updateRel2 (@ Param ("person" ) Person person );
90
132
}
91
133
92
134
@ Service
@@ -105,6 +147,21 @@ public Person updateRel(String from, List<String> languageNames) {
105
147
.collect (Collectors .toList ());
106
148
return personRepository .updateRel (from , knownLanguages );
107
149
}
150
+
151
+ public Optional <Person > updateRel2 (String id , List <String > languageNames ) {
152
+
153
+ Optional <Person > original = personRepository .findById (id );
154
+ if (original .isPresent ()) {
155
+ Person person = original .get ();
156
+ List <Knows > knownLanguages = languageNames .stream ().map (Language ::new )
157
+ .map (language -> new Knows ("Some description" , language ))
158
+ .collect (Collectors .toList ());
159
+ person .setKnownLanguages (knownLanguages );
160
+ return Optional .of (personRepository .updateRel2 (person ));
161
+ }
162
+
163
+ return original ;
164
+ }
108
165
}
109
166
110
167
@ Configuration
0 commit comments