Neo4j SDN-OGM节点平等性
我对Spring数据Neo4j和OGM有问题。当我第一次创建一个节点时,这是正常的,但如果我刷新该页面,就会出现以下错误: 密码执行失败,代码为“Neo.ClientError.Schema.ConstraintValidationFailed”:节点(126)已存在,且标签为Neo4j SDN-OGM节点平等性,neo4j,spring-data-neo4j,neo4j-ogm,Neo4j,Spring Data Neo4j,Neo4j Ogm,我对Spring数据Neo4j和OGM有问题。当我第一次创建一个节点时,这是正常的,但如果我刷新该页面,就会出现以下错误: 密码执行失败,代码为“Neo.ClientError.Schema.ConstraintValidationFailed”:节点(126)已存在,且标签为Country,属性为name=“Country-1” 我在网上搜索了很多关于equals和hashCode的文档,但是没有一个有用。以下是我的课程: public abstract class Place {
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());
}
}
存储库是默认的。据我所知,这是一个平等检查的问题,但我如何解决这个问题?您通过定义
@CompositeIndex(unique=true,properties=“name”)
您可能还在Neo4j OGMSessionFactory
配置中启用了自动索引管理器功能。
这与hashCode
或equals
的任何实现无关
这也将解释您面临的行为:第一次运行成功,但相同的操作重复失败。使用此@CompositeIndex(unique=true,properties=“name”)我将确保不再创建该节点,如果我删除此节点,则每次刷新页面时都会在db中创建相同的节点,我想用ogm的方式来检测,那是同一个节点!在这种情况下,我需要更多的信息当你“刷新页面”时会发生什么。Controller/Service/repository调用或类似调用。@FarjamRamezanpour您是否在同一会话/事务中读取、更改和写入了
Country
节点?您的项目中是否有其他类扩展了Places类。