I am developing a website using spring/hibernate. I have a domain class 'Answer' as below,
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;
-------------------------------------
-------------------------------------
}
So I have One To Many relation among Answer and Comment entities.
To test this I am using below code,
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);
I am getting an existing Answer - 1001. And trying to add 2 comments newly created to that answer's comment collection. And saving that answer object.
When I run this I am receiving the below error,
org.hibernate.LazyInitializationException: could not initialize proxy
- no Session at the line -- commentAnswer.setComment(commentList);
Can someone please explain what am I doing wrong here?
Thanks