首页 > 解决方案 > Neo4j SDN - OGM 节点平等

问题描述

我对 Spring Data Neo4j 和 OGM 有疑问。当我第一次创建节点时,没关系,但是如果我刷新该页面,我会收到此错误:

Cypher 执行失败,代码为“Neo.ClientError.Schema.ConstraintValidationFailed”:节点(126)已经存在,标签Country和属性name=“Country-1”。

我在网上搜索并阅读了很多有关的文件equalshashCode但没有一个有帮助。这是我的课程:

public abstract class Place {

    @Id
    @GeneratedValue(strategy = UuidStrategy.class)
    private String id ;

    private String name ;

    public String getId(){return id ;}

}

@Getter
@Setter
@NodeEntity(label = "Country")
@CompositeIndex(unique = true , properties = "name")
public class Country extends Place {

    private String label = "Country";

    public Country() {}

    @Override
    public int hashCode() {
        int result  = label == null ? 1 : label.hashCode();
        result = 31 * result + this.getName() == null ? 0 : this.getName().hashCode();
        result = 31 * result + this.getId() == null ? 0 : this.getId().hashCode();
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Country)) {
            return false;
        }
        Country that = (Country) o ;
        return this.getName().equals(that.getName())
                && this.getId().equals(that.getId())
                && this.getLabel().equals(that.getLabel());
    }
}

存储库是默认的。据我所知,这是一个平等检查问题,但我该如何解决这个问题?

标签: neo4jspring-data-neo4jneo4j-ogm

解决方案


Country您通过定义为您的实体创建了一个约束

@CompositeIndex(unique = true , properties = "name")

并且可能您还在 Neo4j-OGMSessionFactory配置中启用了自动索引管理器功能。hashCode这与or的任何实现无关equals

这也将解释您所面临的行为:第一次运行成功,但相同的操作重复失败。


推荐阅读