86
86
import org .springframework .data .neo4j .core .mapping .Neo4jMappingContext ;
87
87
import org .springframework .data .neo4j .core .transaction .Neo4jBookmarkManager ;
88
88
import org .springframework .data .neo4j .core .transaction .Neo4jTransactionManager ;
89
+ import org .springframework .data .neo4j .integration .imperative .repositories .FlightRepository ;
89
90
import org .springframework .data .neo4j .integration .imperative .repositories .PersonRepository ;
90
91
import org .springframework .data .neo4j .integration .imperative .repositories .PersonWithNoConstructorRepository ;
91
92
import org .springframework .data .neo4j .integration .imperative .repositories .PersonWithWitherRepository ;
92
93
import org .springframework .data .neo4j .integration .imperative .repositories .ThingRepository ;
93
- import org .springframework .data .neo4j .integration .shared .common .Flight ;
94
- import org .springframework .data .neo4j .integration .imperative .repositories .FlightRepository ;
95
94
import org .springframework .data .neo4j .integration .shared .common .AltHobby ;
96
95
import org .springframework .data .neo4j .integration .shared .common .AltLikedByPersonRelationship ;
97
96
import org .springframework .data .neo4j .integration .shared .common .AltPerson ;
110
109
import org .springframework .data .neo4j .integration .shared .common .EntityWithConvertedId ;
111
110
import org .springframework .data .neo4j .integration .shared .common .EntityWithRelationshipPropertiesPath ;
112
111
import org .springframework .data .neo4j .integration .shared .common .ExtendedParentNode ;
112
+ import org .springframework .data .neo4j .integration .shared .common .Flight ;
113
113
import org .springframework .data .neo4j .integration .shared .common .Friend ;
114
114
import org .springframework .data .neo4j .integration .shared .common .FriendshipRelationship ;
115
115
import org .springframework .data .neo4j .integration .shared .common .Hobby ;
@@ -1909,24 +1909,54 @@ void createWithCustomQueryShouldWorkWithNestedObjects(@Autowired Driver driver,
1909
1909
1910
1910
Assumptions .assumeTrue (ServerVersion .version (driver ).greaterThanOrEqual (ServerVersion .v4_1_0 ));
1911
1911
1912
+ PersonWithRelationship p = createNewPerson ("A Person" , createNewClub ("C27" ));
1913
+
1914
+ PersonWithRelationship newPerson = repository .createWithCustomQuery (p );
1915
+ newPerson = repository .findById (newPerson .getId ()).get ();
1916
+ assertThat (newPerson .getName ()).isEqualTo (p .getName ());
1917
+ assertThat (newPerson .getHobbies ().getName ()).isEqualTo ("A Hobby" );
1918
+ assertThat (newPerson .getPets ()).extracting (Pet ::getName ).containsExactlyInAnyOrder ("A" , "B" );
1919
+ assertThat (newPerson .getClub ().getName ()).isEqualTo ("C27" );
1920
+ }
1921
+
1922
+ private PersonWithRelationship createNewPerson (String name , Club club ) {
1912
1923
PersonWithRelationship p = new PersonWithRelationship ();
1913
- p .setName ("A Person" );
1924
+ p .setName (name );
1914
1925
p .setId (4711L );
1915
1926
Hobby h = new Hobby ();
1916
1927
h .setName ("A Hobby" );
1917
1928
p .setHobbies (h );
1918
1929
p .setPets (Arrays .asList (new Pet ("A" ), new Pet ("B" )));
1919
1930
1920
- Club club = new Club ();
1921
- club .setName ("C27" );
1922
1931
p .setClub (club );
1932
+ return p ;
1933
+ }
1923
1934
1924
- PersonWithRelationship newPerson = repository .createWithCustomQuery (p );
1925
- newPerson = repository .findById (newPerson .getId ()).get ();
1926
- assertThat (newPerson .getName ()).isEqualTo (p .getName ());
1927
- assertThat (newPerson .getHobbies ().getName ()).isEqualTo ("A Hobby" );
1928
- assertThat (newPerson .getPets ()).extracting (Pet ::getName ).containsExactlyInAnyOrder ("A" , "B" );
1929
- assertThat (newPerson .getClub ().getName ()).isEqualTo ("C27" );
1935
+ private Club createNewClub (String name ) {
1936
+ Club club = new Club ();
1937
+ club .setName (name );
1938
+ return club ;
1939
+ }
1940
+
1941
+ @ Test // DATAGRAPH-2292
1942
+ void createWithCustomQueryShouldWorkWithCollectionsOfNestedObjects (@ Autowired Driver driver , @ Autowired RelationshipRepository repository ) {
1943
+
1944
+ Assumptions .assumeTrue (ServerVersion .version (driver ).greaterThanOrEqual (ServerVersion .v4_1_0 ));
1945
+
1946
+ Club c27 = createNewClub ("C27" );
1947
+ Set <PersonWithRelationship > people = new HashSet <>();
1948
+ people .add (createNewPerson ("A person" , c27 ));
1949
+ people .add (createNewPerson ("Another person" , c27 ));
1950
+
1951
+ List <PersonWithRelationship > newPeople = repository .createManyWithCustomQuery (people );
1952
+ assertThat (newPeople ).hasSize (2 )
1953
+ .allSatisfy (p -> {
1954
+ PersonWithRelationship newPerson = repository .findById (p .getId ()).get ();
1955
+ assertThat (newPerson .getName ()).isEqualTo (p .getName ());
1956
+ assertThat (newPerson .getHobbies ().getName ()).isEqualTo ("A Hobby" );
1957
+ assertThat (newPerson .getPets ()).extracting (Pet ::getName ).containsExactlyInAnyOrder ("A" , "B" );
1958
+ assertThat (newPerson .getClub ().getName ()).isEqualTo ("C27" );
1959
+ });
1930
1960
}
1931
1961
1932
1962
@ Test
@@ -1937,8 +1967,7 @@ void saveSingleEntityWithRelationships(@Autowired RelationshipRepository reposit
1937
1967
Hobby hobby = new Hobby ();
1938
1968
hobby .setName ("Music" );
1939
1969
person .setHobbies (hobby );
1940
- Club club = new Club ();
1941
- club .setName ("ClownsClub" );
1970
+ Club club = createNewClub ("ClownsClub" );
1942
1971
person .setClub (club );
1943
1972
Pet pet1 = new Pet ("Jerry" );
1944
1973
Pet pet2 = new Pet ("Tom" );
@@ -4142,6 +4171,37 @@ interface RelationshipRepository extends Neo4jRepository<PersonWithRelationship,
4142
4171
+ "RETURN n, collect(r), collect(p)" )
4143
4172
PersonWithRelationship createWithCustomQuery (PersonWithRelationship p );
4144
4173
4174
+ @ Transactional
4175
+ @ Query ("UNWIND $0 AS pwr WITH pwr CREATE (n:PersonWithRelationship) \n "
4176
+ + "SET n.name = pwr.__properties__.name \n "
4177
+ + "WITH pwr, n, id(n) as parentId\n "
4178
+ + "UNWIND pwr.__properties__.Has as x\n "
4179
+ + "CALL { WITH x, parentId\n "
4180
+ + " \n "
4181
+ + " WITH x, parentId\n "
4182
+ + " MATCH (_) \n "
4183
+ + " WHERE id(_) = parentId AND x.__labels__[0] = 'Pet'\n "
4184
+ + " CREATE (p:Pet {name: x.__properties__.name}) <- [r:Has] - (_)\n "
4185
+ + " RETURN p, r\n "
4186
+ + " \n "
4187
+ + " UNION\n "
4188
+ + " WITH x, parentId\n "
4189
+ + " MATCH (_) \n "
4190
+ + " WHERE id(_) = parentId AND x.__labels__[0] = 'Hobby'\n "
4191
+ + " CREATE (p:Hobby {name: x.__properties__.name}) <- [r:Has] - (_)\n "
4192
+ + " RETURN p, r\n "
4193
+ + "\n "
4194
+ + " UNION\n "
4195
+ + " WITH x, parentId\n "
4196
+ + " MATCH (_) \n "
4197
+ + " WHERE id(_) = parentId AND x.__labels__[0] = 'Club'\n "
4198
+ + " CREATE (p:Club {name: x.__properties__.name}) - [r:Has] -> (_)\n "
4199
+ + " RETURN p, r\n "
4200
+ + "\n "
4201
+ + "}\n "
4202
+ + "RETURN n, collect(r), collect(p)" )
4203
+ List <PersonWithRelationship > createManyWithCustomQuery (Collection <PersonWithRelationship > p );
4204
+
4145
4205
PersonWithRelationship .PersonWithHobby findDistinctByHobbiesName (String hobbyName );
4146
4206
}
4147
4207
0 commit comments