【发布时间】:2014-06-17 04:15:51
【问题描述】:
我真的希望这没有被问及我缺乏搜索技能。我目前正在学习 spring data 如何通过 rest api (spring-data-neo4j-rest) 与 neo4j 独立服务器交互,并在迭代查询结果时遇到了一个有趣的问题。
我首先创建了 Instant Spring Tool Suite 一书中的 neo4j 项目(使用嵌入式数据库),验证它是否工作,将项目升级到 Spring 的最新版本,验证是否工作,然后将其指向一个独立服务器以进行必要的更改。
我能够在我的 JUnit 测试用例中创建节点,但是当针对带有注释查询的方法运行测试用例时,它似乎无法正常工作。我尝试寻找可能遇到此问题的示例和其他人,并确信我的相对经验不足是问题的主要根源。
查询代码如下: @Query("匹配 (x {name: {0}}) findEmployeesAndRolesByProjectName(String prjName);
这是测试用例:
@Transactional
public void shouldReturnEmployeesAndRolesByProject() {
Project projectX = template.save(new Project("Project X"));
Project projectY = template.save(new Project("Project Y"));
Employee johnDoe = template.save(new Employee("John", "Doe"));
johnDoe.assignedTo(projectX, "Software Engineer");
johnDoe.assignedTo(projectY, "Business Analyst");
template.save(johnDoe);
Employee janeDoe = template.save(new Employee("Jane", "Doe"));
janeDoe.assignedTo(projectX, "Project Manager");
template.save(janeDoe);
Iterable<Map<String, String>> projectRoles = projectRepository
.findEmployeesAndRolesByProjectName("Project X");
Map<String, String> role1 = projectRoles.iterator().next();
System.out.print(role1.get("name") + ' ' + role1.get("title") + '\n');
assertEquals("John Doe", role1.get("name"));
assertEquals("Software Engineer", role1.get("title"));
Map<String, String> role2 = projectRoles.iterator().next();
System.out.print(role2.get("name") + ' ' + role2.get("title") + '\n');
assertEquals("Jane Doe", role2.get("name"));
assertEquals("Project Manager", role2.get("title"));
Map<String, String> role3 = projectRoles.iterator().next();
System.out.print(role3.get("name") + ' ' + role3.get("title") + '\n');
assertEquals("John Doe", role3.get("name"));
assertEquals("Software Engineer", role3.get("title"));
}
通过 REST 客户端运行查询时的 json 响应结果如下:
{
columns: [2]
0: "name"
1: "title"
-
data: [2]
0: [2]
0: "John Doe"
1: "Software Engineer"
-
1: [2]
0: "Jane Doe"
1: "Project Manager"
-
-
}
在遍历 Map 结果时,它似乎只提取了第一个 json 响应,导致测试单元失败,因为它期望名称为“Jane Doe”。我添加了打印以查看它在角色 1 和 2 中提取的数据,并且在每种情况下都打印了“John Doe”和“软件工程师”。我更改了测试以查找角色 2 中的 John Doe 记录,并添加了第三次迭代,预计它会失败,因为结果集应该只包含两条记录,但这也会返回 John Doe 记录。
对于冗长的解释我深表歉意,但是谁能指出我阅读查询回复的正确方向?
【问题讨论】:
标签: spring neo4j spring-data-neo4j