Use hibernate to get multiparty information from one party error: org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role

Source: Internet
Author: User

Cause: Hibernate loads the associated object in a lazy way and loads it immediately. If the load method is not specified in a multiple-to-one configuration, and a one-to-many configuration specifies lazy loading, the session is closed when the acquisition party is available, and when a multiparty is obtained, it is not possible to obtain multiple information and therefore an error.

Workaround: Change the one-to-many load mode to immediate load and change the load to lazy loading.

Change the one-to-many annotation configuration to resemble the following:

@OneToMany (mappedby= "topicentity", Fetch = Fetchtype.eager)
Public list<answerentity> getanswerlist () {
return answerlist;
}
public void Setanswerlist (list<answerentity> answerlist) {
This.answerlist = answerlist;
}

Change the multiple-to-one annotation configuration to read as follows:

@ManyToOne (Fetch=fetchtype.lazy,optional=false)
@JoinColumn (name= "TopicID")
Public topicentity gettopicentity () {
return topicentity;
}
public void settopicentity (Topicentity topicentity) {
this.topicentity = topicentity;
}

Use hibernate to get multiparty information from one party error: org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.