2011-11-10 5 views
1

最初のpersist()を削除すると以下のテストは失敗します。 Setをインスタンス化するためにNodeEntityを保持する必要があるのはなぜですか?これを行うにはよりよい方法がありますか?私はネセサリーよりも頻繁にデータベースに書き込む必要はありません。私が言及した行を削除する場合Springデータグラフ内のNeo4J NodeEntity間の関係をpersistを2回呼び出さずに保持する方法

@Test 
public void testCompetenceCreation() { 
    Competence competence = new Competence(); 
    competence.setName("Testcompetence"); 
    competence.persist(); //test fails if this line is removed 
    Competence competenceFromDb = competenceRepository.findOne(competence.getId()); 

    assertEquals(competence.getName(), competenceFromDb.getName()); 

    Education education = new Education(); 
    education.setName("Bachelors Degree"); 
    competence.addEducation(education); 
    competence.persist(); 


    assertEquals(competence.getEducations(), competenceFromDb.getEducations()); 
} 

、例外怒鳴るが発生します。

@JsonIgnoreProperties({"nodeId", "persistentState", "entityState"}) 
@NodeEntity 
public class Education { 

    @GraphId 
    private Long id; 

    @JsonBackReference 
    @RelatedTo(type = "COMPETENCE", elementClass = Competence.class, direction = Direction.INCOMING) 
    private Competence competence; 

    @Indexed 
    private String name; 

    public Long getId() { 
     return id; 
    } 

    public String getName() { 
     return name; 
    } 

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

@JsonIgnoreProperties({"nodeId", "persistentState", "entityState"}) 
@NodeEntity 
public class Competence { 

    @RelatedTo(type = "EDUCATION", elementClass = Education.class) 
    private Set<Education> educations; 

    public Set<Education> getEducations() { 
     return educations; 
    } 

    public void addEducation(Education education) { 
     this.educations.add(education); 
    } 
} 

java.lang.NullPointerException 
at com.x.entity.Competence.addEducation(Competence.java:54) 

Competence.classをスローします

答えて

1

実行中のSDNのバージョンは?

最初の状態が持続するまでエンティティは切り離され、AJはフィールドを処理しません(マネージセットの作成など)。 Persistは、ノードをエンティティに接続するときにノードを作成します。その後、トランザクションがコミットするまでエンティティが接続され、すべての変更が書き込まれます。

これはコミット時にdbに書き込むため、あまりにも多くの書き込みについて心配はありません。他のすべての変更は、トランザクションのためにメモリに保持されます。おそらく、あなたはまた、@Transactionalでテストメソッドに注釈を付けるべきです。

これにはJIRA問題を作成できますか?一貫した取り扱いが提供されるようにする。 (問題は、あなたが設定され、自分自身を初期化するとき、それはおそらく文句ということで。)

他の二つの事柄:

  • 教育< --Competenceとのあなたの関係は、おそらく同じで、ただでナビゲートする必要があるとして他の方向にはと同じtypeという名前を付ける必要があります。
  • 教育< - :あなたはあなたのエンティティが作成されない持続呼び出すと、あなたの迅速な返信マイケルのためnull

+0

感謝を返すことによって、その後findOneない場合は

  • -Competenceも[で提供]。どのようにして永続性を使用する必要があるのか​​を今理解しており、アノテーションのリレーションシップタイプについてのメモも正しいと思います。あなたは私がJira問題に書いてもらいたいことを説明することができますか?私が理解しているように、これはバグではなく、Spring Data Graphの仕組みを完全に理解しているわけではありません:-) –

  • +0

    問題を作成し、このディスカッションにリンクしてください。説明として、コレクションフィールドを一貫性のある方法で初期化します。ありがとう –

    +0

    完了!再度、あなたの助けをありがとう、非常に感謝します。 https://jira.springsource.org/browse/DATAGRAPH-135 –

    関連する問題