Could not initialize Proxy-no Session

Source: Internet
Author: User

workaround for could not initialize proxy-no Session error in Hibernate3 and introduction of lazy load could not initialize Proxy-no Sessio N
Exception: Org.hibernate.LazyInitializationException:could not initialize proxy-no Session

Cause: The default option for Hibernate3 many-to-one is lazy = "Proxy"

Workaround:<many-to-one> and <set> set lazy= "false"

The Hbm.xml configuration is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<class name= "Po. EMP "table=" EMP ">
<id name= "empno" type= "Java.lang.Short" >
<column name= "EMPNO" precision= "4" scale= "0"/>
<generator class= "Increment"/>
</id>
<property name= "ename" type= "java.lang.String" >
<column name= "ename" length= "ten"/>
</property>
<property name= "Job" type= "java.lang.String" >
<column name= "JOB" length= "9"/>
</property>
<property name= "Mgr" type= "Java.lang.Short" >
<column name= "MGR" precision= "4" scale= "0"/>
</property>
<property name= "HireDate" type= "Java.util.Date" >
<column name= "HireDate" length= "7"/>
</property>
<property name= "Sal" type= "java.lang.Double" >
<column name= "SAL" precision= "7"/>
</property>
<property name= "comm" type= "java.lang.Double" >
<column name= "COMM" precision= "7"/>
</property>
<many-to-one name= "Dept" class= "Po. Dept "column=" DEPTNO "></many-to-one>
</class>

Cause of the exception:
When using Hibernatetemplate's save, load, and other methods, its session management strategy is: open session, related operations, by default using lazy load results, no SQL statement, return the proxy class, and then close the session, At this point you get a orgnization proxy class, if you want to invoke the Orgnization instance of the getter method, then it will actually load Orgnization object, issued a SQL statement, can now be closed session, so will throw an exception, Tip Ould Not initialize Proxy-no Session.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Could not initialize Proxy-no Session

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.