How is the error lazyinitializer generated?

Source: Internet
Author: User
From <proficient in hibernate: detailed explanation of Java object persistence technology> author: Sun weiqin Source: www.javathinker.org
If it is reprinted, indicate the source. Thank you.

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.

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 and extends the customer class. Therefore, it inherits all attributes and methods of the customer class, but its implementation is applicable to applications.ProgramIs transparent.
(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 /.

BelowCodeFirst load the customer object through the load () method of the session, and then access its name attribute:

Tx = session. begintransaction ();
Customer customer = (customer) Session. Load (customer. Class, new long (1 ));
Customer. getname ();
TX. Commit ();

In the running session. when the load () method is used, Hibernate does not execute any SELECT statement and only returns the instance of the customer class proxy class. Its oid is 1, which is specified by the second parameter of the load () method. When the application calls the customer. getname () method, Hibernate initializes the customer proxy class instance, loads the data of the customer object from the database, and runs the following SELECT statement:

Select * from MERs where id = 1;
Select * from orders where customer_id = 1;

When the lazy attribute of the <class> element is true, various runtime behaviors of the load () method of the session will be affected. The following is an example.

1. If the loaded customer object does not exist in the database, the load () method of the session will not throw an exception. Only when the customer. getname () method is run will the following exception be thrown:

Error lazyinitializer: 63-exception initializing proxy
Net. SF. hibernate. objectnotfoundexception: no row with the given identifier exists: 1, of class:
Mypack. Customer

2. If the application has not accessed the customer object throughout the session, the instance of the customer proxy class will not be initialized and hibernate will not execute any SELECT statement. The following code attempts to access the customer free object after closing the session:

Tx = session. begintransaction ();
Customer customer = (customer) Session. Load (customer. Class, new long (1 ));
TX. Commit ();
Session. Close ();
Customer. getname ();

Because the instance of the customer proxy class referenced by the customer variable is never initialized within the session range, Hibernate will throw the following exception when executing the customer. getname () method:

Error lazyinitializer: 63-exception initializing proxy
Net. SF. hibernate. hibernateexception: cocould not initialize proxy-the owning session was closed

It can be seen that the instance of the customer proxy class can be initialized only within the current session range.

3.net. SF. hibernate. hibernate class initialize () static method is used to explicitly initialize the proxy class instance within the session range. The isinitialized () method is used to determine whether the proxy class instance has been initialized. For example:

Tx = session. begintransaction ();
Customer customer = (customer) Session. Load (customer. Class, new long (1 ));
If (! Hibernate. isinitialized (customer ))
Hibernate. initialize (customer );
TX. Commit ();
Session. Close ();
Customer. getname ();

The above Code explicitly initializes the customer proxy class instance through the initialize () method of the hibernate class within the scope of the session. Therefore, after the session is closed, the customer free object can be normally accessed.

4. When the application accesses the GETID () method of the proxy instance, The Hibernate initialization of the proxy instance is not triggered, for example:

Tx = session. begintransaction ();
Customer customer = (customer) Session. Load (customer. Class, new long (1 ));
Customer. GETID ();
TX. Commit ();
Session. Close ();
Customer. getname ();

When the application accesses the customer. GETID () method, this method returns the oId value of the customer proxy instance directly without querying the database. Because the referenced variable customer always references a non-initialized customer proxy instance, Hibernate will throw the following exception when the session is closed and then the customer. getname () method is executed:

Error lazyinitializer: 63-exception initializing proxy
Net. SF. hibernate. hibernateexception: cocould not initialize proxy-the owning session was closed

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.