Cocould not initialize proxy-No session

Source: Internet
Author: User

Org. hibernate. lazyinitializationexception: cocould not initialize proxy-No session

Delayed initialization errors are the most common errors in hibernate development projects. If a delayed retrieval policy is configured for a class or set, it can be initialized only when the proxy class instance or proxy set is in the persistent state (that is, within the session range. If it is initialized only when it is in the Free State, a delayed initialization error will occur.

Because when we load an object in hibernate, we use a proxy object. That is to say, when we execute the load method, we do not issue an SQL statement, but return a proxy object. Only when the specific get ** method is used can an SQL statement be sent to the database for query. But when we turn on the session and close the session to the srping, our session will be closed by the srping after we load the session, this error is reported if we use the get method again on the JSP page or other places.

Set the lazy attribute of the <class> element of the customer. HBM. xml file to true, which indicates that the delayed retrieval policy is used:
<Class name = "mypack. Customer" table = "MERs" lazy = "true"

When the load () method of the session is executed, Hibernate does not immediately execute the SELECT statement for querying the MERs table and only returns the instance of the customer class proxy class. This proxy class has the following characteristics:

(1) It is dynamically generated by hibernate at runtime. It extends the customer class, so it inherits all attributes and methods of the customer class, but its implementation is transparent to applications.
(2) When hibernate creates a customer proxy instance, it only initializes its OID attribute, and other attributes are null. Therefore, this proxy instance occupies very little memory.
(3) When the application accesses the customer proxy instance for the first time (for example, the customer is called. getxxx () or customer. setxxx () method), Hibernate will initialize the proxy class instance, execute the SELECT statement during the initialization process, and truly load all data of the customer object from the database. However, when the application accesses the GETID () method of the customer proxy instance, Hibernate does not initialize the proxy instance because the oId exists when the proxy instance is created, you do not have to query data in the database.

Tip: hibernate uses the cglib tool to generate the proxy class of the persistence class. Cglib is a powerful Java bytecode generation tool that dynamically generates extension Java classes or proxies that implement Java interfaces when the program is running. For more information about cglib, see http://cglib.sourceforge.net /.

Solution 1: if we use the get method in hibernate, we can solve the problem of getting a single object, because the get method directly sends an SQL statement, get the desired data from the database and store it in the memory.
There is no problem if we can use the get method to retrieve a single object. However, if the object we get has an associated object, we can use get because it will not retrieve the associated object, however, if the associated object is used on the page, no session is reported.

Solution 2: Use the srping filter (the filter must be added before the strutsfilter, because it also has a sequence and a first-in-first-out principle)

Use this method to resolve object classes using Annotation

Add

<Filter> <filter-Name> opensessioninview </filter-Name> <filter-class> Org. springframework. orm. hibernate3.support. opensessioninviewfilter </filter-class> </filter> <filter-mapping> <filter-Name> opensessioninview </filter-Name> <URL-pattern>/* </url-Pattern> </filter-mapping>
In this way, the opensession closesession is handed over to the view part. When the view part is used up and the session is closed, the above error will not occur.

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.