Neo4j SDN-OGM节点相等

问题描述 投票:0回答:1

我对Spring Data Neo4j和OGM有问题。第一次创建节点时,可以,但是如果刷新该页面,则会出现此错误:

密码执行失败,代码为'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());
    }
}

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

neo4j spring-data-neo4j neo4j-ogm
1个回答
1
投票

[您通过定义Country实体创建了约束

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

并且可能还已在Neo4j-OGM SessionFactory配置中启用了自动索引管理器功能。这与hashCodeequals的任何实现都不相关。

这还将说明您面临的行为:第一次运行成功,但是重复的相同操作失败。

© www.soinside.com 2019 - 2024. All rights reserved.