2016-04-02 9 views
2

既存のノードとの関係で新しいノードを作成します。今すぐ春データのNeo4jは、私は、ユーザーのノードとの関係があるのクラスAの新しいノード、作成する必要があり

@NodeEntity 
public class A implements Serializable { 

    /** 
    * Graph node identifier 
    */ 
    @GraphId 
    private Long nodeId; 

    /** 
    * Enity identifier 
    */ 
    private String id; 

    //... more properties ... 

    @Relationship(type = "HAS_ADVERTISER", direction = Relationship.OUTGOING) 
    private User user; 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (!(o instanceof IdentifiableEntity)) return false; 

     IdentifiableEntity entity = (IdentifiableEntity) o; 
     if (!super.equals(o)) return false; 

     if (id != null) { 
      if (!id.equals(entity.id)) return false; 
     } else { 
      if (entity.id != null) return false; 
     } 

     return true; 
    } 


} 


@NodeEntity 
public class User implements Serializable { 

    /** 
    * Graph node identifier 
    */ 
    @GraphId 
    private Long nodeId; 

    /** 
    * Enity identifier 
    */ 
    private String id; 

    //... more properties ... 


    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (!(o instanceof IdentifiableEntity)) return false; 

     IdentifiableEntity entity = (IdentifiableEntity) o; 
     if (!super.equals(o)) return false; 

     if (id != null) { 
      if (!id.equals(entity.id)) return false; 
     } else { 
      if (entity.id != null) return false; 
     } 

     return true; 
    } 

} 

を、のは、我々を仮定してみましょう私は新しいノードAと「ID」を持っているユーザーの(既存の)ノード間の関係でノードAを作成しようとしてい

{ 
    "id": 1, 
    "nodeId": "0001-0001", 
    "user": { 
      "id": 4, 
      "nodeId": "0002-0002", 
      "name": null, 
      "firstName": null 
      } 
} 

:新しいノードAに対して次のデータを持っています:4と "nodeId": "0002-0002"(一意のノード識別子)ですが、その代わりに、ユーザーノードはフィールド "na 「私」および「firstName」をnullに変更します。

私はそれを作成するためにGraphRepositoryプロキシを使用しています:

@Repository 
public interface ARepository extends GraphRepository<A> { 


} 

は、この更新せずにそれを行うにはどのような方法は、ユーザーのノードとの関係を作り、ありますか?

答えて

0

いいえ、すべての不足している値を入力して保存するか、カスタムクエリを書き込むために、IDでエンティティをリロードする必要があります。

+0

ありがとうございました! –

0

MERGEで行うことができます。 Cypher RefCard

@Repository 
public interface ARepository extends GraphRepository<A> { 

    @Query("MERGE (a:A {id:aId}})-[:HAS_ADVERTISER]-(u:User {id:{userId}})" + 
      "RETURN a") 
    public A createAndGetAd(@Param("aId") String aId, 
          @Param("userId") String userId) 
    } 
関連する問題