Failed to lazily initialize a collection of Role: No session or session was closed (hibernate) Solution

Source: Internet
Author: User
Tags id3

When learning to write OA permission management, obtain all the sub-modules of this module through the obtained parent module on the JSP page.

 

 

JSP code snippet:

 

<C: foreach items = "$ {modules}" Var = "module">

 

<C: foreach items = "$ {module. Children}" Var = "child">

 

<TD >$ {Child. name} </TD>

 

 

 

 

 

Java code:

 

@ Onetoworkflow (mappedby = "parent ")

Public set <module> getchildren (){

Return children;

}

 

The Code does not set delayed loading. The error "failed to lazily initialize a collection of Role: No session or session was closed (hibernate)" is reported on the JSP page.

 

 

Through Baidu, I found that I had made a mistake in delayed loading of hibernate.

After modifying the code

@ Onetomany (mappedby = "parent", fetch = fetchtype. Eager)

Public set <module> getchildren (){

Return children;

}

 

Hibernate: Select module0 _. ID as ID3 _, module0 _. name as name3 _, module0 _. ordernum as ordernum3 _, module0 _. parentid as parentid3 _, module0 _. sn AS sn3 _, module0 _. URL as url3 _ from module module0 _ Where module0 _. parentid is null limit?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Hibernate: Select children0 _. parentid as parentid1 _, children0 _. ID as id1 _, children0 _. ID as id3_0 _, children0 _. name as name3_0 _, children0 _. ordernum as ordernum3_0 _, children0 _. parentid as parentid3_0 _, children0 _. sn AS sn3_0 _, children0 _. URL as url3_0 _ from module children0 _ Where children0 _. parentid =?

Although this method performs a large number of SQL operations in the search module, it is feasible as an exercise in this practice.

 

 

 

The page is displayed normally and the sub-modules of this module can be obtained. By viewing the SQL statement printed by hibernate, we can see that hibernate extracts all its associated sub-modules while extracting the parent module.

 

 

 

To verify @ onetovel (mappedby = "parent", fetch = fetchtype. Lazy)

Public set <module> getchildren (){

Return children;

}

Can it achieve the desired effect? After I modify the code, the JSP page still reports the same error, prompting that the submodule cannot be loaded.

Although delayed loading is set, the delayed loading function can be implemented in Java code. However, when the JSP page is called, the session is closed after the action is passed to the JSP page, and the modules in the JSP page is obtained from the action through the session. When the session has been closed, it is impossible to use modules to delay loading (that is, re-issue the SQL statement to obtain the entity classes of its submodules in the database.

Viewing the SQL statement printed by hibernate confirms that it can only query the attributes of the object class, but cannot query the attributes associated with it.

 

 

 

Hibernate: Select module0 _. ID as ID3 _, module0 _. name as name3 _, module0 _. ordernum as ordernum3 _, module0 _. parentid as parentid3 _, module0 _. sn AS sn3 _, module0 _. URL as url3 _ from module module0 _ Where module0 _. parentid is null limit?

 

 

 

 

 

In order to find out the delayed loading, Baidu has a bit more. In summary, the delayed loading:

A. Delayed loading of object: usually used in the load () method. The proxy class of the object class is returned. During operations on the object class, Hibernate will go to the database to obtain the real object class object.

B. Delayed loading of the set type: the relationship between the parent module and the sub-module mentioned in this Article. Here, we will remind you that the Set set must be used to define the sub-object set in the object class.

C. Delayed attribute loading: Generally, an object class is subdivided and some attributes of the object class are set to delayed loading. However, when performing operations on these attributes, to read the column values of these attributes in the database.

 

 

 

 

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.