2


I am working on an Spring boot + Neo4j application & I am new to graphDb. Problem I am facing is described below,
I want to create unique (Priviledge) node pointing(In relation) to UserRole Node.

In below image you can see duplicate (Priviledge) node is created enter image description here

how can I write a custom cypher to check Priviledge if exist before saving a new one

UserRole Domain: @NodeEntity public class 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;
  }
}
mohit sharma
  • 1,050
  • 10
  • 20

1 Answers1

0

As it can be found in the answer for this question, to update (and not create new instance) you have to use an ID of the node, when invoking save method.

Alternatively (as found in the comments of mentioned question) you should create you own query based on this documentation.

Community
  • 1
  • 1
Marek J
  • 1,364
  • 8
  • 18
  • 33