0

spring/hibernate を使用して Web サイトを開発しています。以下のようなドメインクラス「Answer」があります。

Answer.java

@Entity
public class Answer {
   @OneToMany(fetch=FetchType.EAGER)
   @JoinTable(name="Answer_comment",joinColumns=@JoinColumn(name="ANSWER_ID"),
           inverseJoinColumns=@JoinColumn(name="COMMENT_ID"))
    private Collection<Comment> comment;

   -------------------------------------
   -------------------------------------
}

したがって、Answer エンティティと Comment エンティティの間に一対多の関係があります。

これをテストするために、以下のコードを使用しています。

Answer commentAnswer = answerService.getAnswer(1001);

   Comment comment2 = new Comment();
   comment2.setId(1004);
   comment2.setBody("I cannot agree with your answer..because ...");
   comment2.setUser(userService.getUser("ss"));
   //commentService.create(comment2);

   Comment comment3 = new Comment();
   comment3.setId(1005);
   comment3.setBody(" Yes I agree with your answer...sorry for my previous comment");
   comment3.setUser(userService.getUser("ss"));
   //commentService.create(comment3);

   ArrayList<Comment> commentList = new ArrayList<Comment>();
   commentList.add(comment2);
   commentList.add(comment3);

   commentAnswer.setComment(commentList);

   answerService.editAnswer(commentAnswer);

既存の回答 - 1001 を取得しています。その回答のコメント コレクションに新しく作成された 2 つのコメントを追加しようとしています。そして、その回答オブジェクトを保存します。

これを実行すると、以下のエラーが表示されます。

 org.hibernate.LazyInitializationException: could not initialize proxy 
 - no Session at the line -- commentAnswer.setComment(commentList);

誰かが私がここで間違っていることを説明できますか?

ありがとう

4

1 に答える 1

3

まあ..私はこれを機能させました。AnswerエンティティをロードするためにHibernateTemplate.loadメソッドを使用しています。ここで、AnswerエンティティをロードするためにHibernateTemplate.getメソッドに変更しました。現在動作しています。

于 2012-08-21T20:38:22.710 に答える