-->
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.  [ 3 posts ] 
Author Message
 Post subject: Lazy initialization problem, I'm not sure...
PostPosted: Fri Jul 29, 2005 10:55 am 
Newbie

Joined: Mon Oct 18, 2004 12:29 pm
Posts: 8
Hello everyone,
I posted a topic regarding a weird behavior with a property. I explained that if earlier in the code I made some call like System.out.println(obj.getForward()); the system worked perfectly, otherwise, my struts app would crash later on in a jsp because obj.getForward() returned null (where in fact It isn't null). I thought (and a reply came with the same answer), that it had to do with the lazy initialization of the object. The problem is that the property I'm talking about is a String not a collection, and as far as I know, lazy fetching of individual properties is default to false. (paranoid as I am, I set it to false, and still the same result). Here is the mapping of the Privilege class and others.

Code:
<hibernate-mapping>
   <class name="MPrivilege" table="T_PRIVILEGE">
      <id name="id" type="int" unsaved-value="0">
         <generator class="increment"/>
      </id>

      <property name="name"/>
      <property name="forward"/>

      <bag name="rolPrivilegios" inverse="true" lazy="true" cascade="all-delete-orphan">
         <key column="privilege_id"/>
         <one-to-many class="MRolPrivilege"/>
      </bag>
   </class>
</hibernate-mapping>

<hibernate-mapping>
   <class name="MRolPrivilege" table="T_ROL_PRIVILEGE">

      <id name="id" type="int" unsaved-value="0">
         <generator class="increment"/>
      </id>

      <many-to-one name="privilegioRef" class="MPrivilege" column="privilege_id"/>
                 <many-to-one name="rolRef" class="MRol" column="rol_id"/>
   </class>
</hibernate-mapping>

<hibernate-mapping>
   <class name="MRol" table="T_ROL">

      <id name="id" type="int" unsaved-value="0">
         <generator class="increment"/>
      </id>

      <property name="nombre"/>

      <bag name="rolPrivilege" inverse="true" lazy="true" cascade="all-delete-orphan">
         <key column="rol_id"/>
         <one-to-many class="MRolPrivilege"/>
      </bag>      
   </class>
</hibernate-mapping>


On a Struts Action this is the relevant code
Code:
Iterator itt = rol.getPrivileges().iterator();
        while (itt.hasNext()) {
          MPrivilege privilege = (MPrivilege) itt.next();
          menuElements.add(privilege);
        }


And finally in a jsp I iterate through menuElements vector, and print the privelege.getForward().
Any help is appreciate it
Thanx in advance.


Top
 Profile  
 
 Post subject: Re: Lazy initialization problem, I'm not sure...
PostPosted: Fri Jul 29, 2005 6:07 pm 
Beginner
Beginner

Joined: Mon Apr 12, 2004 6:33 pm
Posts: 35
I had the same problems after migrating to hibernate 3. I set lazy="false" on the class tag (I don't see this in your mapping). Althought the docs say this is the default it solved the problem for me.


Top
 Profile  
 
 Post subject: Re: Lazy initialization problem, I'm not sure...
PostPosted: Fri Jul 29, 2005 9:08 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
kdekooter wrote:
I had the same problems after migrating to hibernate 3. I set lazy="false" on the class tag (I don't see this in your mapping). Althought the docs say this is the default it solved the problem for me.


lazy="false" at the class-level is NOT the default in Hibernate 3.

Hibernate 3 changed the default to lazy=true for associations so that we (users) are forced to think about what associations we need and don't perform any necessary database access.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.