我正在尝试在Neo4j中创建节点之间的关系。我正在使用Neo4J(2.1.8 Community)和spring-data-neo4j(3.3.0.RELEASE)。

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

创建一个新的Employee(节点),它将向数据库中的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中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 + '}';
}
}

最佳答案

请显示员工的定义!

您也可以使用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);
}

07-24 09:49
查看更多