2016-08-26 5 views
1

SpringDataの新しいノードを既存のノードと同じプロパティおよび関係で保存しようとすると、新しいノードを挿入しません。私はヌルIDでそれを保存しています。SpringデータNeo4jは新しいノードを挿入せず、同じプロパティを持つ既存のノードのみを更新します。

問題が何ですか?

のNeo4j 3.0.0
春データ4.1.2
のNeo4j OGM 2.0.2

public abstract class ModelObject { 

    @GraphId 
    protected Long id; 

    //getters and setters 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) 
      return true; 
     if (o == null || id == null || getClass() != o.getClass()) 
      return false; 

     ModelObject entity = (ModelObject) o; 

     if (!id.equals(entity.id)) 
      return false; 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     return (id == null) ? -1 : id.hashCode(); 
    } 

} 


    @RelationshipEntity(type = "COLLECTION") 
public class Collection extends ModelObject{ 

    @DateString("yyyy-MM-dd") 
    private Date acquisitionDate; 
    @StartNode 
    private User collector; 
    @EndNode 
    private Item item; 
    private Boolean manual; 
    private Boolean box; 
    private Double paidValue; 
    private String historyAcquisition; 

    //getters and setters 

} 



@Service 
public class CollectionServiceImpl implements ICollectionService { 

    @Autowired 
    private UserRepo userRepo; 

    @Autowired 
    private CollectionRepo collectionRepo; 

    @Autowired 
    private ItemRepo itemRepo; 

    @Override 
    public Iterable<Collection> findByUserId(Integer idUser) { 
     return collectionRepo.findByCollectorId(idUser); 
    } 

    @Override 
    public boolean addItemCollection(Collection collection, Long itemId) { 

     try { 

     Long userId = collection.getCollector().getId(); 

     collection.setCollector(userRepo.findOne(userId, 1)); 
     collection.setItem(itemRepo.findOne(itemId, 1)); 
     collection.setId(null); 


     collectionRepo.save(collection); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      return false; 
     } 

     return true; 
    } 


    @Override 
    public boolean removeItemCollection(Long collectionId, Long itemId) { 

     try { 

     collectionRepo.delete(collectionId); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      return false; 
     } 

     return true; 
    } 


} 


@NodeEntity(label="USER") 

パブリッククラスユーザーがModelObject {

private String fullName; 
private String userName; 
private String password; 
private Country country; 

@DateString(DateUtil.yyyy_MM_dd) 
private Date birthDate; 

@Relationship(type="FOLLOWING", direction=Relationship.OUTGOING) 
private Set<Following> following; 

@Relationship(type="COLLECTION", direction=Relationship.OUTGOING) 
private List<Collection> collection ; 

}

+0

いくつかのコードを見ずに言うのは難しい – Luanne

+0

私は投稿を編集しました。コードの書式設定は残念です。 基本的には、この保存がaddItemCollection(Collectionコレクション、Long itemId)であるメソッドです。基本的には、SaveメソッドGraphRepository を呼び出します。 ありがとうございます! –

答えて

1

ますので、これはおそらくですが拡張idをnullに明示的に設定します。 OGMセッションはエンティティ参照を追跡し、このケースは無効です。これは、現在null IDを持つ既知の以前に保存されたエンティティです。なぜ保存するために新しいCollectionオブジェクトを作成しないのですか?コメント

SDN/OGMのみプロパティの同じセットを持つ2つの所与のノード間の1つの関係を作成するに基づいて更新

。通常は、一対のノード間で同一のプロパティ値を持つ関係を持つことはあまり価値がありません。記述したようにタイムスタンプを追加することは、グラフモデルに必要な場合に複数の関係を強制する1つの方法です。

+0

こんにちはLuanne、 実際に私は正しく説明しませんでした。このメソッドを呼び出す前に "collection"オブジェクトが作成され、新しいオブジェクトになります。そして、以前はidの値をnullに設定していませんでしたが、これは私が最後に試みたものです。 –

+0

それはうまくいくはずです - おそらく私たちにテストケースを送ってもらえますか? https://github.com/neo4j/neo4j-ogm/issues – Luanne

+0

さて、私は一時的な解決策として何をしましたか?オブジェクトに別のプロパティー「@DateLong private Date registrationDate」を追加しました。常に現在の時刻で挿入するため、以前に永続化されたオブジェクトと同じプロパティ値は繰り返されません。しかし、私はまだ更新の理由を知りたいし、ノードを挿入したくない。 –

関連する問題