-->
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.  [ 7 posts ] 
Author Message
 Post subject: many-to-one lazy loading not working
PostPosted: Fri Sep 30, 2005 2:43 pm 
Newbie

Joined: Fri Sep 30, 2005 2:35 pm
Posts: 5
Hibernate version: 3.05 w/ JBoss 4.02

I just can't seem to get my many-to-one relationship to load lazily. I've reduced it to it's most simple form but it just won't load lazily. I have code that simply looks up 10 InterviewImpl objects and just reads a string property from each one, however each time I read that property a SQL statement is called to retrieve the ApplicationImpl object. I'm never referencing the ApplicationImpl object however so I don't know why a query is sent to the database.

I thought lazy loading was the default for many-to-one relationships in Hibernate 3, but it just doesn't seem to be working. Any help would be greatly appreciated.

Thanks!! Mapping documents below.

Mapping documents:
Code:
    <class
        name="InterviewImpl"
        table="INTERVIEW"
        lazy="true"
    >

        <id
            name="oid"
            column="oid"
            type="long"
            unsaved-value="0"
        >
            <generator class="HiLoGenerator">
            </generator>
        </id>

        <many-to-one
            name="application"
            class="ApplicationImpl"
            cascade="save-update"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            column="APPLICATION_OID"
            not-null="true"
        />

....
    <class
        name="ApplicationImpl"
        table="APPLICATION"
        lazy="true"
    >

        <id
            name="oid"
            column="oid"
            type="long"
            unsaved-value="0"
        >
            <generator class="HiLoGenerator">

            </generator>
        </id>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 3:22 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
You need to specify it on the association between your two objects:

<many-to-one
name="application"
class="ApplicationImpl"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
access="property"
column="APPLICATION_OID"
not-null="true"
lazy="true"
/>

http://www.hibernate.org/hib_docs/v3/re ... -manytoone

Quote:
lazy (optional - defaults to proxy): By default, single point associations are proxied. lazy="true" specifies that the property should be fetched lazily when the instance variable is first accessed (requires build-time bytecode instrumentation). lazy="false" specifies that the association will always be eagerly fetched.



Let me know if it helped.

Regards,
Vincent.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 3:36 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Actually,
You are right, proxy means true.

You are using lazy initialization.

You should not load the associated object.

Post your routine that reads the parent object.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 4:48 pm 
Newbie

Joined: Fri Sep 30, 2005 2:35 pm
Posts: 5
I tried adding lazy="true" to the associating, but still to no avail. Here's some simple code I put together to reduce it to its most simple form.

Code:
try {
   SessionFactory factory = (SessionFactory)InitialContextFactory.getInitialContext().lookup("java:/jobwired/HibernateFactory");
   Session session = factory.getCurrentSession();
   List l = session.createCriteria(InterviewImpl.class).list();
   Iterator it = l.iterator();
   while (it.hasNext()) {
      InterviewImpl interview = (InterviewImpl)it.next();
      System.out.println(interview.getInterviewerName());
   }
} catch (Exception e) {
   e.printStackTrace();
}


getInterviewerName simple returns a string property that is stored in the INTERVIEW table. And here is the output when called... As you can see it's still eagerly fetching.

Code:
13:48:49,888 INFO  [STDOUT] Hibernate: select this_.oid as oid0_, this_.APPLICATION_OID as APPLICAT2_262_0_, this_.ORG_PRINCIPAL_OID as ORG3_262_0_, this_.LOCATION_OID as LOCATION4_262_0_, this_.INTERVIEW_DATE as INTERVIEW5_262_0_, this_.NOTES as NOTES262_0_, this_.TYPE as TYPE262_0_, this_.RATING as RATING262_0_, this_.INTERVIEWER as INTERVIE9_262_0_, this_.INTERVIEW_ID as INTERVIEW10_262_0_, this_.INTERVIEWER_NAME as INTERVI11_262_0_, this_.FEEDBACK_RECEIVED as FEEDBACK12_262_0_, this_.FEEDBACK_REQUEST_SENT as FEEDBACK13_262_0_, this_.INVITE_SENT as INVITE14_262_0_, this_.REMINDER_SENT as REMINDER15_262_0_, this_.LOCATION_DETAILS as LOCATION16_262_0_, this_.CANDIDATE_NAME as CANDIDATE17_262_0_, this_.POSITION_TITLE as POSITION18_262_0_ from INTERVIEW this_
13:48:50,817 INFO  [STDOUT] Hibernate: select applicatio0_.oid as oid0_, applicatio0_.EXAM_SESSION_OID as EXAM2_258_0_, applicatio0_.POSITION_OPENING_OID as POSITION3_258_0_, applicatio0_.SOURCE_POSITION_OPENING_OID as SOURCE4_258_0_, applicatio0_.PROFILE_OID as PROFILE5_258_0_, applicatio0_.COMPANY_OID as COMPANY6_258_0_, applicatio0_.START_TIME as START7_258_0_, applicatio0_.SUBMIT_TIME as SUBMIT8_258_0_, applicatio0_.EXAM_COMPLETE as EXAM9_258_0_, applicatio0_.SUBMITTED as SUBMITTED258_0_ from APPLICATION applicatio0_ where applicatio0_.oid=?
13:48:50,857 INFO  [STDOUT] Hibernate: select applicatio0_.oid as oid0_, applicatio0_.EXAM_SESSION_OID as EXAM2_258_0_, applicatio0_.POSITION_OPENING_OID as POSITION3_258_0_, applicatio0_.SOURCE_POSITION_OPENING_OID as SOURCE4_258_0_, applicatio0_.PROFILE_OID as PROFILE5_258_0_, applicatio0_.COMPANY_OID as COMPANY6_258_0_, applicatio0_.START_TIME as START7_258_0_, applicatio0_.SUBMIT_TIME as SUBMIT8_258_0_, applicatio0_.EXAM_COMPLETE as EXAM9_258_0_, applicatio0_.SUBMITTED as SUBMITTED258_0_ from APPLICATION applicatio0_ where applicatio0_.oid=?
13:48:50,867 INFO  [STDOUT] Hibernate: select applicatio0_.oid as oid0_, applicatio0_.EXAM_SESSION_OID as EXAM2_258_0_, applicatio0_.POSITION_OPENING_OID as POSITION3_258_0_, applicatio0_.SOURCE_POSITION_OPENING_OID as SOURCE4_258_0_, applicatio0_.PROFILE_OID as PROFILE5_258_0_, applicatio0_.COMPANY_OID as COMPANY6_258_0_, applicatio0_.START_TIME as START7_258_0_, applicatio0_.SUBMIT_TIME as SUBMIT8_258_0_, applicatio0_.EXAM_COMPLETE as EXAM9_258_0_, applicatio0_.SUBMITTED as SUBMITTED258_0_ from APPLICATION applicatio0_ where applicatio0_.oid=?
13:48:51,047 INFO  [STDOUT] Hibernate: select applicatio0_.oid as oid0_, applicatio0_.EXAM_SESSION_OID as EXAM2_258_0_, applicatio0_.POSITION_OPENING_OID as POSITION3_258_0_, applicatio0_.SOURCE_POSITION_OPENING_OID as SOURCE4_258_0_, applicatio0_.PROFILE_OID as PROFILE5_258_0_, applicatio0_.COMPANY_OID as COMPANY6_258_0_, applicatio0_.START_TIME as START7_258_0_, applicatio0_.SUBMIT_TIME as SUBMIT8_258_0_, applicatio0_.EXAM_COMPLETE as EXAM9_258_0_, applicatio0_.SUBMITTED as SUBMITTED258_0_ from APPLICATION applicatio0_ where applicatio0_.oid=?
13:48:51,796 INFO  [STDOUT] Hibernate: select applicatio0_.oid as oid0_, applicatio0_.EXAM_SESSION_OID as EXAM2_258_0_, applicatio0_.POSITION_OPENING_OID as POSITION3_258_0_, applicatio0_.SOURCE_POSITION_OPENING_OID as SOURCE4_258_0_, applicatio0_.PROFILE_OID as PROFILE5_258_0_, applicatio0_.COMPANY_OID as COMPANY6_258_0_, applicatio0_.START_TIME as START7_258_0_, applicatio0_.SUBMIT_TIME as SUBMIT8_258_0_, applicatio0_.EXAM_COMPLETE as EXAM9_258_0_, applicatio0_.SUBMITTED as SUBMITTED258_0_ from APPLICATION applicatio0_ where applicatio0_.oid=?


Top
 Profile  
 
 Post subject: SOLVED
PostPosted: Sat Oct 08, 2005 8:57 pm 
Newbie

Joined: Fri Sep 30, 2005 2:35 pm
Posts: 5
I thought I would follow up this post with the solution in case it can help anyone else out. As it turns out in the "set" metnod of my resume object I was checking for null, I wouldn't have thought this would be triggered since I never explicitly called the set method my self, but apparent hibernate does when it populates the object, makes sense, this was causing the additional query to the database thereby making it seem like lazy initilization was not working. When I removed the null check the additoinal query that I was seeing went away.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 08, 2005 11:48 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
I have to set nothing for lazy loading for >= 3.0
Try hql - maybe criteria make any join and you have this result

I don't use criteria and I don't know, but I know that hql and lazy loading work fine


Top
 Profile  
 
 Post subject: Same many-to-one problem for me too
PostPosted: Wed Oct 26, 2005 1:26 pm 
Newbie

Joined: Mon Jun 20, 2005 8:40 pm
Posts: 3
Just as a follow up to others who may be reading this thread, I've encountered the exact situation described by impony above. The only difference is that I wasn't able to diffuse it by any coding change in my domain/model objects. The only solution I have is to detach these objects from one another by getting rid of the many-to-one altogether, something that I have the luxury of doing in this particular case.

To this point, hibernate has been interpreting the lazy associations in my app perfectly, when starting from the parent and moving to the child. Going the opposite direction, from child to parent, it seems to have a mind of it's own. I will re-post if I discover a solution.


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