-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: LazyInitializationException: could not initialize proxy
PostPosted: Sat Dec 15, 2007 11:08 am 
Newbie

Joined: Tue Nov 27, 2007 6:11 am
Posts: 12
Hi,
I'm trying to write an application based on hibernate 3, I'm using the cglib2.1.3 and when I run my application I got the following exception:

Code:
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
   at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
   at g2.database.mapping.Person$$EnhancerByCGLIB$$c482508f.getName(<generated>)
   at main.Main.main(Main.java:26)


The application is very simple: I've got a person table and a role table, and the following are the mappings:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="g2.database.mapping">
   <class
      name="Person"
      table="person"
   >
      <meta attribute="sync-DAO">true</meta>
      <id
         name="Id"
         type="string"
         column="personid"
      >
         <generator class="sequence"/>
      </id>

      <property
         name="Personpk"
         column="personpk"
         type="integer"
         not-null="true"
         length="10"
      />
      <property
         name="Name"
         column="name"
         type="string"
         not-null="false"
         length="20"
      />
      <property
         name="Surname"
         column="surname"
         type="string"
         not-null="false"
         length="20"
      />
      <property
         name="Borndate"
         column="borndate"
         type="date"
         not-null="false"
         length="13"
      />
      <property
         name="Phone"
         column="phone"
         type="string"
         not-null="false"
         length="50"
      />
      <property
         name="Mobilephone"
         column="mobilephone"
         type="string"
         not-null="false"
         length="50"
      />
      <property
         name="Fax"
         column="fax"
         type="string"
         not-null="false"
         length="50"
      />
      <property
         name="Email"
         column="email"
         type="string"
         not-null="false"
         length="50"
      />
      <property
         name="Web"
         column="web"
         type="string"
         not-null="false"
         length="100"
      />
      <property
         name="RecordDate"
         column="record_date"
         type="date"
         not-null="false"
         length="13"
      />
   


    <set name="roles" table="j_person_role" >
        <key column="personpk"/>
        <many-to-many column="rolepk"
            unique="true"
            class="Role"  />
    </set>

     

   </class>   
</hibernate-mapping>




<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="g2.database.mapping">
   <class
      name="Role"
      table="role"
   >
      <meta attribute="sync-DAO">true</meta>
      <id
         name="Id"
         type="integer"
         column="rolepk"
      >
         <generator class="sequence"/>
      </id>

      <property
         name="Roleid"
         column="roleid"
         type="string"
         not-null="true"
         length="50"
      />
      <property
         name="Rolerevision"
         column="rolerevision"
         type="java.lang.Long"
         not-null="true"
         length="10"
      />
      <property
         name="Description"
         column="description"
         type="string"
         not-null="false"
         length="100"
      />
      <property
         name="Revisioncomment"
         column="revisioncomment"
         type="string"
         not-null="false"
         length="100"
      />
      <property
         name="Creationdate"
         column="creationdate"
         type="date"
         not-null="false"
         length="13"
      />
      <property
         name="Revisiondate"
         column="revisiondate"
         type="date"
         not-null="false"
         length="13"
      />
   



   </class>   
</hibernate-mapping>


If I set all the classes and collection with lazy="false" the application works. Is there a way to use the lazy loading? Please note that I'm not using any container. Moreover, is it suggested to use lazy loading for standard java applications?

Thanks,
Luca


Top
 Profile  
 
 Post subject: Re: LazyInitializationException: could not initialize proxy
PostPosted: Mon Dec 17, 2007 12:28 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
what is the code that causes this problem?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 1:16 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
dude

you have closed the connection and you are trying to access the proxy object which is no fully initialized; because it needs an open connection for initilization.

check your code buddy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 3:41 am 
Newbie

Joined: Tue Nov 27, 2007 6:11 am
Posts: 12
msj4u wrote:
dude

you have closed the connection and you are trying to access the proxy object which is no fully initialized; because it needs an open connection for initilization.

check your code buddy


How can you say that I've closed the connection? Can you be more specific?
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 7:34 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Above posters are saying u that u have closed the connection before initializing the child...

for eg.
Session session = getSession(); //May be from ur super class or utility class
Person person = (Person) session.load()// normal way..
session.close();

so after this line if you try to invoke,
parent.getRoles(), you will have lazyInitializationException...To avoid this can specify it in the mapping file. or before the session.close() line insert the below line.
Hibernate.intialize(parent.getRoles());

Hope this helps:-)

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.