【问题标题】:Neo4j SDN - OGM nodes equalityNeo4j SDN - OGM 节点平等
【发布时间】:2020-02-03 09:01:22
【问题描述】:

我对 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());
    }
}

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

【问题讨论】:

  • 嘿,您的项目中是否还有其他类正在扩展 Places 类。

标签: neo4j spring-data-neo4j neo4j-ogm


【解决方案1】:

您通过定义在Country 实体上创建了一个约束

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

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

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

【讨论】:

  • 有了这个 @CompositeIndex(unique = true , properties = "name") 我确定该节点没有再次创建,如果我删除它,每次在 db 中创建相同的节点,我都会刷新页面,我想通过 ogm 检测到是同一个节点!
  • 在这种情况下,我需要更多信息,当您“刷新页面”时会发生什么。控制器/服务/存储库调用或类似的。
  • @FarjamRamezanpour 您是否在同一会话/事务中读取、更改和写入了 Country 节点?
猜你喜欢
  • 2017-05-24
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多