2016-03-08 26 views
0

我想使用spring-data-neo4j版本4.0.0.RELEASE在Neo4J數據庫中持久保留下面的類。它是一個名爲'GroupCategory'的類,有一些字段,如name,ownerId等。它覆蓋了eclipse框架提供的equals和hashcode方法。重寫hashCode()方法不允許保存Neo4J中的類屬性

@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)); 


} 

三個「printlns」的答案給出如下

----------- 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=[]] 

如果「名稱」字段看了一下,這canbe觀察到字段保存和更新了前兩個案例。但在第三種情況下,在重置爲'Group_0'的前一個值時,名稱字段未保存。 測試用例中的最後一個斷言失敗,因爲保存的對象保留了以前的'Group1'值。這三個printlns會給出這個想法。

恰巧,名稱字段從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; 
} 

但同樣的問題persits其他領域也是如此。至於現在看來,如果hashCode()在類中被覆蓋,交替更新值(值1到值2,然後再次到值1)似乎不工作

回答

2

此錯誤已修復,請使用1.1版。 neo4j-ogm 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> 
+0

我正在使用的pom.xml以下依賴性。 { spring-data-neo4j 4.0.0.RELEASE}我需要切換到neo4j-ogm嗎?如果你可以評論使用哪一個(spring-data-neo4j或neo4j-ogm)以及爲什麼會這樣,那將會很棒。 – Soumya

+0

更新了我的答案 – Luanne