使用现有节点

时间:2015-06-22 05:25:26

标签: java neo4j spring-data-neo4j

我正在尝试在Neo4j中创建节点之间的关系。我正在使用 Neo4J(2.1.8社区)&的弹簧 - 数据 - 的Neo4j(3.3.0.RELEASE)

我正在尝试创建以下关系。

创建一个新的Employee(节点),它将向DB(按名称搜索)中的Manager(节点)报告(空关系)。我使用了以下查询。

public interface EmployeeRepository extends GraphRepository<Employee> {

@Query("START employee=node:({0}), manager=node:Employee(name={1}) CREATE employee-[:REPORTS_TO]->manager")
void addNewEmployee(Employee employee, String managerName);}

我收到了以下错误。

Caused by: org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement START employee=node:({0}), manager=node:Employee(name={1}) CREATE employee-[:REPORTS_TO]->manager; nested exception is Invalid input '(': expected whitespace or an identifier (line 1, column 21)
"START employee=node:({0}), manager=node:Employee(name={1}) CREATE employee-[:REPORTS_TO]->manager"
                     ^

任何人都可以告诉我这个查询有什么问题。此外,如果这不是使用 GraphRepository 创建关系的正确方法,那么我还可以使用其他方法来实现相同的目标。

提前致谢。

注意:我使用this来学习Spring Data for Neo4j中的查询。他们在哪里展示了基本的查询。

已更新:员工类

@NodeEntity
public class Employee {

@GraphId
private Long id;
private String name;
private String department;

@RelatedTo(type = "REPORTS_TO")
private Employee reportsTo;

@RelatedTo(type = "REPORTS_TO", direction = Direction.INCOMING)
Set<Employee> directReport;

public Employee() {
}

public Employee(String name, String department) {
    this.name = name;
    this.department = department;
}

public Long getId() {
    return id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDepartment() {
    return department;
}

public void setDepartment(String department) {
    this.department = department;
}

public Employee getReportsTo() {
    return reportsTo;
}

public void setReportsTo(Employee reportsTo) {
    this.reportsTo = reportsTo;
}

public Set<Employee> getDirectReport() {
    return directReport;
}

public void setDirectReport(Set<Employee> directReport) {
    this.directReport = directReport;
}

@Override
public int hashCode() {
    return super.hashCode();
}

@Override
public boolean equals(Object obj) {
    return super.equals(obj);
}

@Override
public String toString() {
    return "Employee{" + "id=" + id + ", name=" + name + ", department=" + department + '}';
}
}

1 个答案:

答案 0 :(得分:1)

请显示员工的定义!

您也可以使用template.createRelationshipBetween(nodeOrEntity,nodeOrEntity2,type)

对于您的示例,这应该有效:

公共接口EmployeeRepository扩展了GraphRepository {

@Query("MATCH (employee:Employee), (manager:Employee) 
       WHERE id(employee) = {0} AND manager.name = {1}
       CREATE employee-[:REPORTS_TO]->manager")
void addNewEmployee(Employee employee, String managerName);
}

或许更好地使用员工作为员工的数据容器/地图,或直接传递名称:

@Query("MATCH (employee:Employee), (manager:Employee) 
       WHERE employee.name = {0}.name AND manager.name = {1}
       CREATE employee-[:REPORTS_TO]->manager")
void addNewEmployee(Employee employee, String managerName);
}