Lazy loading problem in ssh org.hibernate.LazyInitializationException

Source: Internet
Author: User

When an appeal exception occurs, it is mostly because the service layer gets the object associated to multiple objects, and the rest of the objects are not instantiated in the service layer. If there are multiple students in a class, only the class is checked out in the service layer. The problem arises when an iterative call to a class student in the display layer is reported org.hibernate.LazyInitializationException, because the session is closed and the student's proxy object is used. There are two solutions: one is to use Opensessioninviewfilter (notice that it is configured before the STRUTS2 filter), and the second is to instantiate the student object in the service layer.

The first method is simple and easy to do, but it greatly affects efficiency. The second kind of method can be more troublesome when it encounters more related objects.

Here is an introduction to how Opensessioninviewfilter works:

Hibernate allows lazy loading of associated objects, properties, but must ensure that deferred loading is limited to the same Hibernate Session range. If the Service layer returns a domain object that has the lazy load feature enabled to the Web layer, when the Web tier accesses data that requires lazy loading, the Hibernate Session that loads the realm object is closed, which results in an access exception that delays loading the data. The Opensessioninviewfilter filter provided by spring provides us with a good solution to this problem. The main function of Opensessioninviewfilter is to bind each request process to a Hibernate Session, even if the initial transaction is complete, and can be deferred loading at the WEB layer. The Opensessioninviewfilter filter binds the Hibernate Session to the request thread, which is automatically detected by Spring's transaction manager. Therefore, the opensessioninviewfilter is suitable for the service layer environment using Hibernatetransactionmanager or Jtatransactionmanager for transaction management. It can also be used in non-transactional read-only data operations.
Request-->open session--> Open Connection, start transaction-----render (close connection, session)-->response Some of these procedures are omitted, not very concerned.

Lazy loading problem in ssh org.hibernate.LazyInitializationException

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.