Skip to content

Commit 4351795

Browse files
Add converter from java.util.Map to org.neo4j.driver.Value.
Closes #2576.
1 parent 4083c5d commit 4351795

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

src/main/java/org/springframework/data/neo4j/core/convert/AdditionalTypes.java

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.HashSet;
3333
import java.util.List;
3434
import java.util.Locale;
35+
import java.util.Map;
3536
import java.util.Set;
3637
import java.util.TimeZone;
3738
import java.util.UUID;
@@ -102,10 +103,15 @@ final class AdditionalTypes {
102103
hlp.add(ConverterBuilder.reading(Value.class, Entity.class, Value::asEntity));
103104
hlp.add(ConverterBuilder.reading(Value.class, Node.class, Value::asNode));
104105
hlp.add(ConverterBuilder.reading(Value.class, Relationship.class, Value::asRelationship));
106+
hlp.add(ConverterBuilder.reading(Value.class, Map.class, Value::asMap).andWriting(AdditionalTypes::value));
105107

106108
CONVERTERS = Collections.unmodifiableList(hlp);
107109
}
108110

111+
static Value value(Map<?, ?> map) {
112+
return Values.value(map);
113+
}
114+
109115
static TimeZone asTimeZone(Value value) {
110116
return TimeZone.getTimeZone(value.asString());
111117
}

src/test/java/org/springframework/data/neo4j/integration/issues/IssuesIT.java

+32
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.neo4j.driver.QueryRunner;
4848
import org.neo4j.driver.Session;
4949
import org.neo4j.driver.Transaction;
50+
import org.neo4j.driver.Values;
5051
import org.springframework.beans.factory.annotation.Autowired;
5152
import org.springframework.context.annotation.Bean;
5253
import org.springframework.context.annotation.ComponentScan;
@@ -115,6 +116,9 @@
115116
import org.springframework.data.neo4j.integration.issues.gh2542.TestNodeRepository;
116117
import org.springframework.data.neo4j.integration.issues.gh2572.GH2572Repository;
117118
import org.springframework.data.neo4j.integration.issues.gh2572.GH2572Child;
119+
import org.springframework.data.neo4j.integration.issues.gh2576.College;
120+
import org.springframework.data.neo4j.integration.issues.gh2576.CollegeRepository;
121+
import org.springframework.data.neo4j.integration.issues.gh2576.Student;
118122
import org.springframework.data.neo4j.integration.misc.ConcreteImplementationTwo;
119123
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
120124
import org.springframework.data.neo4j.repository.query.QueryFragmentsAndParameters;
@@ -834,6 +838,34 @@ void getOneShouldNotFailWithoutMatchingRootNodes(@Autowired GH2572Repository GH2
834838
assertThat(gh2572Child).isNull();
835839
}
836840

841+
@Test
842+
@Tag("GH-2576")
843+
void listOfMapsShouldBeUsableAsArguments(@Autowired Neo4jTemplate template, @Autowired CollegeRepository collegeRepository) {
844+
845+
var student = template.save(new Student("S1"));
846+
var college = template.save(new College("C1"));
847+
848+
var pair = Map.of("stuGuid", student.getGuid(), "collegeGuid", college.getGuid());
849+
var listOfPairs = List.of(pair);
850+
851+
var uuids = collegeRepository.addStudentToCollege(listOfPairs);
852+
assertThat(uuids).containsExactly(student.getGuid());
853+
}
854+
855+
@Test
856+
@Tag("GH-2576")
857+
void listOfMapsShouldBeUsableAsArgumentsWithWorkaround(@Autowired Neo4jTemplate template, @Autowired CollegeRepository collegeRepository) {
858+
859+
var student = template.save(new Student("S1"));
860+
var college = template.save(new College("C1"));
861+
862+
var pair = Map.of("stuGuid", student.getGuid(), "collegeGuid", college.getGuid());
863+
var listOfPairs = List.of(Values.value(pair));
864+
865+
var uuids = collegeRepository.addStudentToCollegeWorkaround(listOfPairs);
866+
assertThat(uuids).containsExactly(student.getGuid());
867+
}
868+
837869
@Configuration
838870
@EnableTransactionManagement
839871
@EnableNeo4jRepositories(namedQueriesLocation = "more-custom-queries.properties")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2011-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2576;
17+
18+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
19+
import org.springframework.data.neo4j.core.schema.Id;
20+
import org.springframework.data.neo4j.core.schema.Node;
21+
import org.springframework.data.neo4j.core.support.UUIDStringGenerator;
22+
23+
/**
24+
* @author Michael J. Simons
25+
*/
26+
@Node
27+
public class College {
28+
29+
@Id @GeneratedValue(UUIDStringGenerator.class)
30+
private String guid;
31+
32+
private String name;
33+
34+
public College(String name) {
35+
this.name = name;
36+
}
37+
38+
public String getGuid() {
39+
return guid;
40+
}
41+
42+
public String getName() {
43+
return name;
44+
}
45+
46+
public void setName(String name) {
47+
this.name = name;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2011-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2576;
17+
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
import org.neo4j.driver.Value;
22+
import org.springframework.data.neo4j.repository.Neo4jRepository;
23+
import org.springframework.data.neo4j.repository.query.Query;
24+
25+
/**
26+
* @author Michael J. Simons
27+
*/
28+
public interface CollegeRepository extends Neo4jRepository<College, String> {
29+
30+
@Query("""
31+
UNWIND $0 AS row
32+
MATCH (student:Student{guid:row.stuGuid})
33+
MATCH (college:College{guid:row.collegeGuid})
34+
CREATE (student)<-[:STUDENT_OF]-(college) RETURN student.guid"""
35+
)
36+
List<String> addStudentToCollege(List<Map<String, String>> list);
37+
38+
@Query("""
39+
UNWIND $0 AS row
40+
MATCH (student:Student{guid:row.stuGuid})
41+
MATCH (college:College{guid:row.collegeGuid})
42+
CREATE (student)<-[:STUDENT_OF]-(college) RETURN student.guid"""
43+
)
44+
List<String> addStudentToCollegeWorkaround(List<Value> list);
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2011-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2576;
17+
18+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
19+
import org.springframework.data.neo4j.core.schema.Id;
20+
import org.springframework.data.neo4j.core.schema.Node;
21+
import org.springframework.data.neo4j.core.schema.Relationship;
22+
import org.springframework.data.neo4j.core.support.UUIDStringGenerator;
23+
24+
/**
25+
* @author Michael J. Simons
26+
*/
27+
@Node
28+
public class Student {
29+
30+
@Id @GeneratedValue(UUIDStringGenerator.class)
31+
private String guid;
32+
33+
private String name;
34+
35+
@Relationship(type = "STUDENT_OF", direction = Relationship.Direction.OUTGOING)
36+
private College college;
37+
38+
public Student(String name) {
39+
this.name = name;
40+
}
41+
42+
public String getGuid() {
43+
return guid;
44+
}
45+
46+
public String getName() {
47+
return name;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
54+
public College getCollege() {
55+
return college;
56+
}
57+
58+
public void setCollege(College college) {
59+
this.college = college;
60+
}
61+
}

0 commit comments

Comments
 (0)