3

My Project is based on Spring boot + Neo4j . I am trying to create a new Privilege node , but don't want to duplicate Privilege.

Now I have a UserRole node which is holds List<Privilege>. Now I want that when I create a Privilege , it check first is another Privilege exists with same privilegeName property.

Below are my domain classes.

UserRole Class

 @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;
      }
    }

Privilege Class

  @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;
      }
    }

I am Using GraphRepository to save Entities.

Luanne
  • 19,145
  • 1
  • 39
  • 51
mohit sharma
  • 1,050
  • 10
  • 20

1 Answers1

4

The only way to do this currently is to query for the Privilege existing first and then create it if not, or use it if it does. Also add a unique constraint to be safe.

In a future release, this use case will be supported.

Luanne
  • 19,145
  • 1
  • 39
  • 51
  • thank u for your insight, but where is need to write the code to check if Privilege already exisits, because UserRole Repository is being used to create priviledge. Can you share an example of such kind ? – mohit sharma Sep 01 '16 at 06:24
  • Before creating a Privilege and adding it to UserRole? – Luanne Sep 01 '16 at 06:26