neo4j - Neo4j SDN - OGM 节点平等
问题描述
我对 Spring Data Neo4j 和 OGM 有疑问。当我第一次创建节点时,没关系,但是如果我刷新该页面,我会收到此错误:
Cypher 执行失败,代码为“Neo.ClientError.Schema.ConstraintValidationFailed”:节点(126)已经存在,标签
Country
和属性name
=“Country-1”。
我在网上搜索并阅读了很多有关的文件equals
,hashCode
但没有一个有帮助。这是我的课程:
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());
}
}
存储库是默认的。据我所知,这是一个平等检查问题,但我该如何解决这个问题?
解决方案
Country
您通过定义为您的实体创建了一个约束
@CompositeIndex(unique = true , properties = "name")
并且可能您还在 Neo4j-OGMSessionFactory
配置中启用了自动索引管理器功能。hashCode
这与or的任何实现无关equals
。
这也将解释您所面临的行为:第一次运行成功,但相同的操作重复失败。
推荐阅读
- java - 如何在循环中更新标签
- ubuntu - 使用来自 Cloudflare 的 .pem 和 .key 设置 Ubuntu Apache2 SSL
- python - 使用字典和贪心算法解决 Set Cover 问题时的返回键
- java - 为什么 equals() 方法不能按预期工作?
- javascript - 如何在 2020 年使用纯 javascript 从 id 获取 YouTube 嵌入视频标题?
- php - 登录后回显受保护的内容
- ansible - 列表的 Ansible/YAML 格式不一致?
- sql-server - Vim dadbod 配置适配器
- javascript - 将日期从 UTC 转换为 EST(Javascript、HTML)
- keycloak - 通过 CLI 为 Keycloak 配置 reCAPTCHA