-->
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.  [ 11 posts ] 
Author Message
 Post subject: Map marked as lazy="false" is lazy instanciated
PostPosted: Mon Apr 18, 2005 10:49 am 
Regular
Regular

Joined: Fri Feb 13, 2004 10:02 pm
Posts: 90
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.01
Mapping documents:
Code:
<hibernate-mapping package="com.llic.calc.label">
  <class name="Label" table="Label">
 
    <id name="id" column="labelid" type="java.lang.Long">
      <generator class="native" />
    </id>
       
    <property name="name" column="name" type="string" unique="true"/>
   
   <many-to-one name="equation" class="com.llic.calc.variable.EquationVariable" lazy="false"/>
   
   <!-- map of database labels -->
   <map name="dbVar" lazy="false">
      <key column="labelid"/>
      <map-key type="java.lang.Long" column="dbvarid"/>
      <element type="java.lang.Long" column="rowId"/>
   </map>
   
  </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
String hql = "from " + getBusinessClass() + " as label where label.name = :name";

List results = getHibernateTemplate().findByNamedParam(hql, "name", name);

return (Label) getFirstResult(results);

Full stack trace of any exception that occurs:
Code:
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:80)
   at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
   at com.llic.calc.variable.EquationVariable$$EnhancerByCGLIB$$95da3f81.getVariables(<generated>)
   at com.llic.calc.LabelPreparer.prepareEquation(LabelPreparer.java:48)
   at com.llic.calc.LabelPreparer.applyLabel(LabelPreparer.java:38)
   at com.llic.calc.EquationFactory.getLabeledEquation(EquationFactory.java:224)
   at com.llic.calc.LabelingTest.testLabelAdd(LabelingTest.java:81)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)

Name and version of the database you are using:
MySQL 4.0
The generated SQL (show_sql=true):
Code:
select dbvar0_.labelid as labelid__, dbvar0_.rowId as rowId__, dbvar0_.dbvarid as dbvarid__ from ides_dbVar dbvar0_ where dbvar0_.labelid=?




Hibernate is lazy loading the map. I explicity set lazy="false" in the mapping, since I do not want to lazy load the map. The session is closed when the map is utilized in our logic, and we will always use all of the entries. Should I report this as a bug in Jira, or did I simply overlook something obvious?
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 11:20 am 
Newbie

Joined: Wed Mar 02, 2005 6:28 pm
Posts: 13
well, I think you don't understent clearly what "lazy" means.

lazy="true"
-> you load the data only if you need it.
-> you can access the data only while in persistent mode (after new Transaction and before Transaction.commit() )

lazy="false"
-> you are loading the whole state of the object when you load it , including all elements/values of the map.

I hope it was a help for you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 11:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
forget about lazy and focus on fetch attribute:
fetch="select" means lazy behaviour
fetch="join" means eager loading behaviour

change
Code:
   <map name="dbVar" lazy="false">
      <key column="labelid"/>
      <map-key type="java.lang.Long" column="dbvarid"/>
      <element type="java.lang.Long" column="rowId"/>
   </map>

to
Code:
   <map name="dbVar" fetch="join">
      <key column="labelid"/>
      <map-key type="java.lang.Long" column="dbvarid"/>
      <element type="java.lang.Long" column="rowId"/>
   </map>

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 12:00 pm 
Regular
Regular

Joined: Fri Feb 13, 2004 10:02 pm
Posts: 90
Anthony,
Thanks for the help, I've been using hibernate since version 2.x, where the lazy attribute was capable of specifying this behavior. Will the documentation to collections be updated for Hibernate 3 to relfect this change in mapping?

Todd


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 12:03 pm 
Regular
Regular

Joined: Fri Feb 13, 2004 10:02 pm
Posts: 90
No dice. I'm still getting the same error. As I said before, the map needs to be loaded completely for access outside of the session scope. I need to have the non-lazy load behavior, and it seems that all attributes are not working correctly


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 12:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Don't believe you. I got unit tests that show this stuff is all working.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 1:00 pm 
Regular
Regular

Joined: Fri Feb 13, 2004 10:02 pm
Posts: 90
Ok, Gavin was correct, the problem does lie somehere else. If you look at the original stacktrace

Code:
at com.llic.calc.variable.EquationVariable$$EnhancerByCGLIB$$95da3f81.getVariables(<generated>)
   at com.llic.calc.LabelPreparer.prepareEquation(LabelPreparer.java:48)


You can see that the problem comes when label.getEquation().getVariables is called. It is nothing wrong with the map, however, the many-to-one relation is being lazy loaded when I have specified lazy="false". I have also tried replacing lazy="false" with fetch="join". It appears that I am getting a cglib proxy for my EquationVariable instance, shouldn't lazy="false" just get me the loaded POJO? I have attached my EquationVariable mapping.

Code:
<hibernate-mapping package="com.llic.calc.variable">
  <joined-subclass name="EquationVariable" table="EquationVariable" extends="Variable">
     
      <key column="variableId"/>
   
         
      <property name="expressionString" column="expressionString" type="string" length="500"/>
   
      <map name="expressionVariables" cascade="all" lazy="false">
         <key column="equationVariableId"/>
         <map-key  type="string" column="variableName"/>
         <one-to-many class="Variable"/>      
      </map>
   
      <!-- custom persister -->
   <property name="returnType" column="returnType" type="com.llic.calc.dao.hibernate.type.InterfacePersister"/>
   
   
  </joined-subclass>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 1:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Read the documentation and understand the difference between

<many-to-one lazy="false"/>

and

<class lazy="false">


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 1:34 pm 
Regular
Regular

Joined: Fri Feb 13, 2004 10:02 pm
Posts: 90
OK, from my understanding of reading the documentation here is what I am trying to do.

All properties of class Label need to be eagerly loaded. I can either set lazy="false" at the class level of class com.llic.calc.label.Label, or set lazy="false" on each property. I have tried to set lazy="false" globally, and I am still receiving the cglib proxy exception when I call label.getEquation(). What the heck am I doing wrong? I've never had this much trouble getting mappings correct, and this is driving me nuts! I thought that by setting lazy="false" at the class level for com.llic.calc.label.Label, that all properties would be intanciated when the Label class is instanciated. Obviously I'm making a mistake, do you see anything obvious?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 1:50 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Listen, you need to go and buy yourself a copy of Hibernate in Action and read it, then check out the diffs between HB2 and HB3 and then try to understand before posting frustrated semi-understood problem descriptions here.

P.S. You can solve your problem trivially just by mapping

<class name="EquationVariable" lazy="false">


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 1:54 pm 
Regular
Regular

Joined: Fri Feb 13, 2004 10:02 pm
Posts: 90
Gavin,
I do have hibernate in action, its a good book ;-). I tried setting the EquationVariable to lazy, but I received errors about proxy instanciation due to it being a joined subclass. I will review the book on joined-sublcass objects as well as lazy instanciation and try to get a grasp on this.

Todd


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.