-->
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.  [ 1 post ] 
Author Message
 Post subject: LazyInitialization & Sidestepping the CGLIB Proxy
PostPosted: Fri Feb 23, 2007 4:50 am 
Newbie

Joined: Tue Aug 01, 2006 4:17 am
Posts: 8
Hi guys

I really just need some help / clarification on the lazy loading machanism. I understand the benefits of lazy loading and fully agree with them. However there are time when the full object graph is required. In our case we load the object graph, JAXB it to XML and then send it across a webservice, so you can imagine, we need the whole thing up front!!

I did some reading and in the documentation you reccomend using a Criteria query and setting the mode to Fetch.JOIN at the highest level, thus resulting in the cascade loading of all the children up front.

Here id the model I am working with..

Case hasA SET of Applications
Application hasA Policy ( one to one )
Application hasA SET of Entities

here is the code I call to attempt to load the entire graph
Code:
public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Transaction tx = session.beginTransaction();
                Criteria criteria = session.createCriteria(CaseHBM.class);
                criteria.setFetchMode("applications", FetchMode.JOIN);
                criteria.add(Restrictions.isNotNull("caseId"));
                Case appCase = (Case) criteria.uniqueResult();

                tx.commit();

                return appCase;
            }


as you can see, i set the Fetch.JOIN property on the getApplications property, expecting an entirely populated graph to be returned. It works like a charm....except for my one-2-one mapping on the policy object. This still returns a proxy instance to me and I can't for the life of me figure out why?? The set mapping of Entities is returned fully populated all the way down its graph...

that's where you guys come in... please help

see below for the mapping etc...

Hibernate version:3.0.5

Mapping documents:

Application Object Mapping

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="persistence.application.ApplicationHBM"
        proxy="application.Application" table="NB_APPLICATION">
        <id name="applicationId" type="long">
            <column name="APPL_ID" precision="10" scale="0" />
            <generator class="sequence">
                <param name="sequence">NB_GENERIC_SEQ</param>
            </generator>
        </id>
       
        <many-to-one name="policy" class="persistence.policy.PolicyHBM" cascade="all"
            column="POLICY_ID" unique="true" />

<set name="persistableEntities" table="NB_APP_ENTITY_CONN" cascade="all">
            <key column="APPL_ID" not-null="true" />
            <many-to-many column="ENTITY_ID" unique="true"
                class="persistence.entity.applicant.ApplicantEntityHBM" />
        </set>

...blah blah blah ( nothing of significance here )


    </class>
</hibernate-mapping>

Policy Object Mapping
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name=".persistence.policy.PolicyHBM"
      proxy="policy.Policy" table="NB_POLICY" >
      <id name="pk" type="long">
         <column name="ID" precision="10" scale="0" />
         <generator class="sequence">
            <param name="sequence">NB_GENERIC_SEQ</param>
         </generator>
      </id>
       
   ...blah blah blah ( nothing of significance here )
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
Code:
public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Transaction tx = session.beginTransaction();
                Criteria criteria = session.createCriteria(CaseHBM.class);
                criteria.setFetchMode("applications", FetchMode.JOIN);
                criteria.add(Restrictions.isNotNull("caseId"));
                Case appCase = (Case) criteria.uniqueResult();

                tx.commit();

                return appCase;
            }



Full stack trace of any exception that occurs:

10:16:04,017 ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session was closed
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:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at org.hibernate.proxy.HibernateProxy$$EnhancerByCGLIB$$6b2f7ae.hashCode(<generated>)
at java.util.HashMap.hash(HashMap.java:261)
at java.util.HashMap.containsKey(HashMap.java:339)
at java.util.HashSet.contains(HashSet.java:180)
at org.apache.commons.lang.builder.ReflectionToStringBuilder.isRegistered(ReflectionToStringBuilder.java:133)
at org.apache.commons.lang.builder.ToStringStyle.appendInternal(ToStringStyle.java:345)
at org.apache.commons.lang.builder.ToStringStyle.append(ToStringStyle.java:319)
at org.apache.commons.lang.builder.ToStringBuilder.append(ToStringBuilder.java:825)
at za.co.generic.newbusiness.domain.application.ApplicationImpl.toString(ApplicationImpl.java:376)
at java.lang.String.valueOf(String.java:2131)
at java.util.AbstractCollection.toString(AbstractCollection.java:455)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:223)
at java.lang.String.valueOf(String.java:2131)
at java.lang.StringBuffer.append(StringBuffer.java:370)
at org.apache.commons.lang.builder.ToStringStyle.appendDetail(ToStringStyle.java:457)
at org.apache.commons.lang.builder.ToStringStyle.appendInternal(ToStringStyle.java:351)
at org.apache.commons.lang.builder.ToStringStyle.append(ToStringStyle.java:319)
at org.apache.commons.lang.builder.ToStringBuilder.append(ToStringBuilder.java:825)
at za.co.generic.newbusiness.domain.application.ApplicationCaseImpl.toString(ApplicationCaseImpl.java:132)
at za.co.generic.newbusiness.persistence.application.TestObjectGraphPersistence.testLoad(TestObjectGraphPersistence.java:64)
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 org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
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.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
10:16:04,046 INFO SessionFactoryImpl:776 - closing



Name and version of the database you are using: HSQLDB 1.8.0


Debug level Hibernate log excerpt:
10:15:43,916 DEBUG DefaultLoadEventListener:143 - loading entity: [persistence.policy.PolicyHBM#3]
10:15:43,921 DEBUG DefaultLoadEventListener:216 - creating new proxy for entity


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

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.