Note that there are some explanatory texts on larger screens.

plurals
  1. POorg.hibernate.LazyInitializationException: could not initialize proxy - no Session exception while trying to add collection
    primarykey
    data
    text
    <p>I am developing a website using spring/hibernate. I have a domain class 'Answer' as below,</p> <h2>Answer.java</h2> <pre><code>@Entity public class Answer { @OneToMany(fetch=FetchType.EAGER) @JoinTable(name="Answer_comment",joinColumns=@JoinColumn(name="ANSWER_ID"), inverseJoinColumns=@JoinColumn(name="COMMENT_ID")) private Collection&lt;Comment&gt; comment; ------------------------------------- ------------------------------------- } </code></pre> <p>So I have One To Many relation among Answer and Comment entities. </p> <p>To test this I am using below code,</p> <pre><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&lt;Comment&gt; commentList = new ArrayList&lt;Comment&gt;(); commentList.add(comment2); commentList.add(comment3); commentAnswer.setComment(commentList); answerService.editAnswer(commentAnswer); </code></pre> <p>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.</p> <p>When I run this I am receiving the below error,</p> <pre><code> org.hibernate.LazyInitializationException: could not initialize proxy - no Session at the line -- commentAnswer.setComment(commentList); </code></pre> <p>Can someone please explain what am I doing wrong here?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload