0

それは基本的に私のコードです:

// Start first transaction to create a school

// Here I set a student for the school
school.setStudent(new Student(id));

// I Save

// Then I Start a SECOND transaction to retrieve the same school

// I can read the childs without prolem. 
school.getStudent().getLanguage().getId();

さて、何らかの理由で、上記のすべてを同じトランザクションで実行したいと考えています。それが起こることです:

// Start the transaction to create a school

// Here I set a student that exist in the DB for that new school
school.setStudent(new Student(id));

// I Save

// In the same transaction I retrieve a list of schools, and one of the schools is the newly created.

// In the newly created school I can't access the childs: null pointer exception on language
school.getStudent().getLanguage().getId(); //ERRROR

最初のケースで何が起こるかというと、学生の ID のみを設定したとしても、学生が新しくないため、学生の他のすべての情報がすでに DB に保持されているということです。学校との関係を作りたいだけです。

しかし、2 番目のケースでは、まだトランザクションを終了していません。何らかの理由で、schhol に再度クエリを実行すると、学生には ID のみが含まれ、他のすべての情報は含まれません。言語は確かに null であり、例外を生成します。

session.flush() を追加しようとしましたが、問題は解決しません。

何か考えはありますか?

4

1 に答える 1