15
15
*/
16
16
package org .springframework .data .neo4j .integration .imperative ;
17
17
18
+ import org .junit .jupiter .api .BeforeEach ;
18
19
import org .junit .jupiter .api .Test ;
19
20
import org .neo4j .driver .Driver ;
21
+ import org .neo4j .driver .Record ;
22
+ import org .neo4j .driver .Session ;
20
23
import org .springframework .beans .factory .annotation .Autowired ;
21
24
import org .springframework .context .annotation .Bean ;
22
25
import org .springframework .context .annotation .Configuration ;
36
39
import org .springframework .data .neo4j .test .Neo4jIntegrationTest ;
37
40
import org .springframework .transaction .annotation .EnableTransactionManagement ;
38
41
42
+ import java .util .ArrayList ;
39
43
import java .util .Arrays ;
40
44
import java .util .Collection ;
41
45
import java .util .Collections ;
@@ -54,6 +58,19 @@ public class ImmutableGeneratedIdsIT {
54
58
55
59
protected static Neo4jExtension .Neo4jConnectionSupport neo4jConnectionSupport ;
56
60
61
+ private final Driver driver ;
62
+
63
+ public ImmutableGeneratedIdsIT (@ Autowired Driver driver ) {
64
+ this .driver = driver ;
65
+ }
66
+
67
+ @ BeforeEach
68
+ void cleanUp () {
69
+ try (Session session = driver .session ()) {
70
+ session .run ("MATCH (n) DETACH DELETE n" ).consume ();
71
+ }
72
+ }
73
+
57
74
@ Test // GH-2141
58
75
void saveWithGeneratedIdsReturnsObjectWithIdSet (
59
76
@ Autowired ImmutablePersonWithGeneratedIdRepository repository ) {
@@ -279,7 +296,6 @@ void saveRelationshipWithGeneratedIdsContainsAllRelationshipTypes(
279
296
assertThat (savedPerson .relationshipPropertiesDynamicCollection .values ().iterator ().next ().get (0 ).target .id ).isNotNull ();
280
297
}
281
298
282
- interface ImmutablePersonWithGeneratedIdRepository extends Neo4jRepository <ImmutablePersonWithGeneratedId , Long > {}
283
299
284
300
@ Test // GH-2148
285
301
void childrenShouldNotBeRecreatedForNoReasons (@ Autowired Neo4jTemplate template ) {
@@ -295,6 +311,38 @@ void childrenShouldNotBeRecreatedForNoReasons(@Autowired Neo4jTemplate template)
295
311
assertThat (saved .getChildren ()).allMatch (c -> c .getId () != null && children .contains (c ));
296
312
}
297
313
314
+ @ Test // GH-2223
315
+ void saveWithGeneratedIdsWithMultipleRelationshipsToOneNode (
316
+ @ Autowired ImmutablePersonWithGeneratedIdRepository repository ) {
317
+
318
+ ImmutablePersonWithGeneratedId person1 = new ImmutablePersonWithGeneratedId ();
319
+ ImmutablePersonWithGeneratedId person2 = ImmutablePersonWithGeneratedId .fallback (person1 );
320
+ List <ImmutablePersonWithGeneratedId > onboardedBy = new ArrayList <>();
321
+ onboardedBy .add (person1 );
322
+ onboardedBy .add (person2 );
323
+ ImmutablePersonWithGeneratedId person3 = ImmutablePersonWithGeneratedId .wasOnboardedBy (onboardedBy );
324
+
325
+ ImmutablePersonWithGeneratedId savedPerson = repository .save (person3 );
326
+ assertThat (savedPerson .id ).isNotNull ();
327
+ assertThat (savedPerson .wasOnboardedBy ).allMatch (ob -> ob .id != null );
328
+
329
+ ImmutablePersonWithGeneratedId savedPerson2 = savedPerson .wasOnboardedBy .stream ().filter (p -> p .fallback != null ).findFirst ().get ();
330
+ assertThat (savedPerson2 .fallback .id ).isNotNull ();
331
+
332
+ try (Session session = driver .session ()) {
333
+ List <Record > result = session .run (
334
+ "MATCH (person3:ImmutablePersonWithGeneratedId) " +
335
+ "-[:ONBOARDED_BY]->(person2:ImmutablePersonWithGeneratedId) " +
336
+ "-[:FALLBACK]->(person1:ImmutablePersonWithGeneratedId), " +
337
+ "(person3)-[:ONBOARDED_BY]->(person1) " +
338
+ "return person3" )
339
+ .list ();
340
+ assertThat (result ).hasSize (1 );
341
+ }
342
+ }
343
+
344
+ interface ImmutablePersonWithGeneratedIdRepository extends Neo4jRepository <ImmutablePersonWithGeneratedId , Long > {}
345
+
298
346
@ Configuration
299
347
@ EnableNeo4jRepositories (considerNestedRepositories = true )
300
348
@ EnableTransactionManagement
0 commit comments