重写hashCode()方法不允许在Neo4J中保存类属性

时间:2016-03-08 11:59:06

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

我正在尝试使用spring-data-neo4j版本4.0.0.RELEASE在Neo4J数据库中保留以下类。这是一个名为' GroupCategory'有一些字段,如name,ownerId等。它已经覆盖了eclipse框架提供的equals和hashcode方法。

@NodeEntity(label="GroupCategory")
public class GroupCategory implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @GraphId
    private Long id;
    @Property private String uuid;
    @Property private String name;
    @Property private String creatorUuid;
    @Property private String ownerUuid;
    @Property private String profile;
    @Property private String status;

    @Relationship(type = GroupRelationshipNames.BELONGS_TO, direction = Relationship.INCOMING)
    private List<GroupCategoryRelation> groupCategoryRelations = new ArrayList<GroupCategoryRelation>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCreatorUuid() {
        return creatorUuid;
    }

    public void setCreatorUuid(String creatorUuid) {
        this.creatorUuid = creatorUuid;
    }

    public String getOwnerUuid() {
        return ownerUuid;
    }

    public void setOwnerUuid(String ownerUuid) {
        this.ownerUuid = ownerUuid;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<GroupCategoryRelation> getGroupCategoryRelations() {
        return groupCategoryRelations;
    }

    public void setGroupCategoryRelations(
            List<GroupCategoryRelation> groupCategoryRelations) {
        this.groupCategoryRelations = groupCategoryRelations;
    }

    @Override
    public String toString() {
        return "GroupCategory [id=" + id + ", uuid=" + uuid + ", name=" + name
                + ", creatorUuid=" + creatorUuid + ", ownerUuid=" + ownerUuid
                + ", profile=" + profile + ", status=" + status
                + ", groupCategoryRelations=" + groupCategoryRelations + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((creatorUuid == null) ? 0 : creatorUuid.hashCode());
        result = prime
                * result
                + ((groupCategoryRelations == null) ? 0
                        : groupCategoryRelations.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result
                + ((ownerUuid == null) ? 0 : ownerUuid.hashCode());
        result = prime * result + ((profile == null) ? 0 : profile.hashCode());
        result = prime * result + ((status == null) ? 0 : status.hashCode());
        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        GroupCategory other = (GroupCategory) obj;
        if (creatorUuid == null) {
            if (other.creatorUuid != null)
                return false;
        } else if (!creatorUuid.equals(other.creatorUuid))
            return false;
        if (groupCategoryRelations == null) {
            if (other.groupCategoryRelations != null)
                return false;
        } else if (!groupCategoryRelations.equals(other.groupCategoryRelations))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (ownerUuid == null) {
            if (other.ownerUuid != null)
                return false;
        } else if (!ownerUuid.equals(other.ownerUuid))
            return false;
        if (profile == null) {
            if (other.profile != null)
                return false;
        } else if (!profile.equals(other.profile))
            return false;
        if (status == null) {
            if (other.status != null)
                return false;
        } else if (!status.equals(other.status))
            return false;
        if (uuid == null) {
            if (other.uuid != null)
                return false;
        } else if (!uuid.equals(other.uuid))
            return false;
        return true;
    }



}

我有一个测试用例,它首先保存并稍后更新对象的某些属性,如下所示。测试用例如下

@Test
public void changeCategoryName(){
    String ownerUuid = "PERSON_0_UUID";
    String name = "GROUP_0";
    String name1 = "GROUP_1";

    GroupCategory groupCategory = new GroupCategory();
    groupCategory.setName(name);
    groupCategory.setOwnerUuid(ownerUuid);
    groupCategory.setProfile(Profile.PRIVATE.name());
    GroupCategory savedGroupCategory = groupCategoryService.create(groupCategory);

    System.out.println("----------- "+groupCategoryService.findByUuid(savedGroupCategory.getUuid()));
    Assert.assertTrue(groupCategoryService.findByUuid(savedGroupCategory.getUuid()).getName().equals(name));

    savedGroupCategory.setName(name1);
    savedGroupCategory = groupCategoryService.save(savedGroupCategory);
    System.out.println("----------- "+groupCategoryService.findByUuid(savedGroupCategory.getUuid()));
    Assert.assertTrue(groupCategoryService.findByUuid(savedGroupCategory.getUuid()).getName().equals(name1));


    savedGroupCategory.setName(name);
    groupCategoryService.save(savedGroupCategory);
    System.out.println("----------- "+groupCategoryService.findByUuid(savedGroupCategory.getUuid()));
    Assert.assertTrue(groupCategoryService.findByUuid(savedGroupCategory.getUuid()).getName().equals(name));


}

三张照片的答案&#39;如下所示

----------- GroupCategory [id=889, uuid=9f891006-3d89-4665-ae2f-4946d13b74ac, name=GROUP_0, creatorUuid=null, ownerUuid=PERSON_0_UUID, profile=PRIVATE, status=ACTIVE, groupCategoryRelations=[]]
----------- GroupCategory [id=889, uuid=9f891006-3d89-4665-ae2f-4946d13b74ac, name=GROUP_1, creatorUuid=null, ownerUuid=PERSON_0_UUID, profile=PRIVATE, status=ACTIVE, groupCategoryRelations=[]]
----------- GroupCategory [id=889, uuid=9f891006-3d89-4665-ae2f-4946d13b74ac, name=GROUP_1, creatorUuid=null, ownerUuid=PERSON_0_UUID, profile=PRIVATE, status=ACTIVE, groupCategoryRelations=[]]

如果&#39;名称&#39;在查看字段时,可以观察到前两个案例的字段已保存并更新。但在第三种情况下,在重置为&#39; Group_0&#39;的前一个值时,不会保存名称字段。 测试用例中的最后一个断言失败,因为保存的对象保留了Group1&#39;的先前值。三个印刷品将给出这个想法。

恰好,名称字段的值从Group_0到Group_1的转换有效,但是从Group_1回到Group_0的那个没有。如果该值更改为Group_1中除Group_0之外的任何其他值,则该值将更新。也就是说,如果值正在改变

,则保存不起作用

如果我从GroupCategory类中删除hashCode()方法,那么所有方法似乎都运行良好。特别是只注释名称字段的哈希码就像

一样
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((creatorUuid == null) ? 0 : creatorUuid.hashCode());
    result = prime
            * result
            + ((groupCategoryRelations == null) ? 0
                    : groupCategoryRelations.hashCode());
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    //result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result
            + ((ownerUuid == null) ? 0 : ownerUuid.hashCode());
    result = prime * result + ((profile == null) ? 0 : profile.hashCode());
    result = prime * result + ((status == null) ? 0 : status.hashCode());
    result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
    return result;
}

但同样的问题也存在于其他领域。至于现在似乎,如果在类中重写hashCode(),则替换值(value1到value2,然后再次更改为value1)似乎不起作用

1 个答案:

答案 0 :(得分:2)

此错误已修复,请使用neo4j-ogm版本1.1.6。

<强>更新

SDN取决于Neo4j OGM库。 SDN 4.0与旧版本的Neo4j-OGM一起发布,不包含此错误修复。 您可以添加此依赖项以覆盖Neo4j OGM的版本 -

 <dependency>
       <groupId>org.neo4j</groupId>
       <artifactId>neo4j-ogm</artifactId>
       <version>1.1.6</version>
   </dependency>