如何在创建新节点之前检查节点是否已存在neo4j

时间:2016-09-01 10:58:52

标签: spring-boot neo4j spring-data-neo4j


我正在开发Spring boot + Neo4j应用程序&我是graphDb的新手。 我面临的问题描述如下,
我想创建指向UserRole节点的唯一(Priviledge)节点(相关)。

在下图中,您可以看到创建了重复(Priviledge)节点 enter image description here

如何在保存新的

之前编写自定义密码以检查Priviledge是否存在

UserRole域名:     @NodeEntity     公共类UserRole {

public UserRole(User user, Role role) {
    this.user = user;
    this.role = role;
}

/**
 For Jackson Parsing
 **/
public UserRole() {
}

@GraphId
private Long id;

public UserRole(User user, Role role, Unit unit) {
    this.user = user;
    this.role = role;
    this.unit = unit;
}



public long getId() {
    return id;
}

@Relationship(type = HAS_USERROLE,direction = "OUTGOING")
User user;
public User getUser() {
    return user;
}

@Relationship (type = HAS_ROLE_OF,direction = "OUTGOING")
Role role;
public Role getRole() {
    return role;
}


@Relationship(type = "WORKS_IN",direction = "OUTGOING")
Unit unit;

public Unit getUnit() {
    return unit;
}

public void setUnit(Unit unit) {
    this.unit = unit;
}

@Relationship(type = "HAS_PRIVILEDGE", direction = "OUTGOING")
List<Priviledge> priviledgeList;

public List<Priviledge> getPriviledgeList() {
    return priviledgeList;
}

public void setPriviledgeList(List<Priviledge> priviledgeList) {
    this.priviledgeList = priviledgeList;
}
}

Priviledge Domain:

public class Priviledge {

@GraphId
Long id;


private String priviledge;

private String priviledgeOn;
private Long priviledgeOnId;

public Priviledge() {
}

public Priviledge(String priviledge, String priviledgeOn) {
    this.priviledge = priviledge;
    this.priviledgeOn = priviledgeOn;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getPriviledge() {
    return priviledge;
}

public void setPriviledge(String priviledge) {
    this.priviledge = priviledge;
}

public String getPriviledgeOn() {
    return priviledgeOn;
}

public void setPriviledgeOn(String priviledgeOn) {
    this.priviledgeOn = priviledgeOn;
}

public Long getPriviledgeOnId() {
    return priviledgeOnId;
}

  public void setPriviledgeOnId(Long priviledgeOnId) {
    this.priviledgeOnId = priviledgeOnId;
  }
}

1 个答案:

答案 0 :(得分:0)

正如在question的答案中可以找到的那样,要更新(而不是创建新实例),在调用save方法时必须使用节点的ID。

或者(如上述问题的评论中所示),您应该根据此documentation创建自己的查询。