我真的希望这个问题没有被问到,我的搜索技能也缺乏。目前,我正在学习spring数据如何通过rest (Spring-Data-ne4j-rest)与neo4j独立服务器交互,并且在迭代查询结果时遇到了一个有趣的问题。
我首先创建neo4j项目(使用embedded db),在“即时Spring工具套件”( Instant Spring Tool Suite )一书中找到该项目,验证它是否有效,将项目升级到Spring的最新版本,验证该项目是否有效,然后将其指向独立服务器进行必要的更改。
我能够在我的JUnit测试用例中创建节点,但是当对带有注释查询的方法运行测试用例时,它似乎不能正常工作。我试着寻找可能遇到这个问题的例子和其他人,我相信这是我相对缺乏经验的问题的主要来源。
以下是查询代码:@ query ( "MATCH (x { name:{0}})“)<-role:MEMBER_OF-(Employee)”+“返回employee.firstName +‘’+ employee.lastName作为名称,”+"role.title as title") Iterable> 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“和”“。我更改了测试以查找角色2中的John记录,并添加了第3次迭代,希望它失败,因为结果集应该只包含两个记录,但这也会返回John记录。
我为这个冗长的解释道歉,但是,谁能为我指出正确的方向来读取查询的答复呢?
发布于 2014-04-30 19:02:27
这不对。您正在重新创建迭代器,它总是从一开始就开始。
Map<String, String> role1 = projectRoles.iterator().next();这样做吧:
Iterator<Map<String, String>> it = projectRoles.iterator()
Map<String, String> role1 = it.next();
Map<String, String> role2 = it.next();
assertFalse(it.hasNext());P.S: SDN over REST不是一个优化的解决方案,您可能希望将它更改为服务器扩展,或者保留在嵌入式数据库中。
下面是一个例子:http://inserpio.wordpress.com/2014/04/30/extending-the-neo4j-server-with-spring-data-neo4j/
https://stackoverflow.com/questions/23394930
复制相似问题