我的项目基于Spring boot + Neo4j。
我正在尝试创建一个新的Privilege节点,但不想复制Privilege。

现在,我有一个拥有List<Privilege>的UserRole节点。现在
我希望在创建Privilege时首先检查是否存在另一个具有相同privilegeName属性的Privilege。

以下是我的域类。

UserRole类别

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


礼遇等级

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


我正在使用GraphRepository保存实体。

最佳答案

当前执行此操作的唯一方法是先查询现有的权限,然后查询(如果没有)创建它,或者如果存在则使用它。还添加唯一约束以确保安全。

在将来的版本中,将支持此用例。

07-24 09:33