-->
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: help needed with lazy instantiation
PostPosted: Wed Dec 22, 2004 11:59 am 
Newbie

Joined: Wed Dec 22, 2004 11:36 am
Posts: 11
I am new to Hibernate and can't seem to get lazy instantiation to work. I have a ServiceClient (in my client table) that has a List of ServiceRequests (in my request table). I want to retreive a ServiceClient without initializing the list of requests (there could be hundreds). The list is lazy but the ServiceRequests objects are being loaded anyways. Any ideas?

thx

Hibernate version:
2.1.7

Mapping documents:
Code:
<class name="testservice.ServiceClient" table="client">
      <id name="id" type="integer" unsaved-value="0">
         <generator class="identity"/>
      </id>
      <property name="name" column="name"/>
      <list name="requests" inverse="true" lazy="true">
         <key column="client_id"/>
         <index column="id"/>
         <one-to-many class="testservice.ServiceRequest"/>
      </list>
   </class>

       <class name="testservice.ServiceRequest" table="request">
      <id name="id" type="integer" unsaved-value="0">
         <generator class="identity"/>
      </id>
      <property name="ipAddress" column="ip_address"/>
      <property name="username" column="username"/>
      <property name="createDate" column="create_dt"/>
      <property name="modifiedDate" column="mod_dt"/>
      <many-to-one name="client" class="testservice.ServiceClient" column="client_id" not-null="true"/>
   </class>


Code between sessionFactory.openSession() and session.close():
Code:
Query q = sess.createQuery("from testservice.ServiceClient sc where sc.name = :name");
q.setString("name",client);
ServiceClient sc = (ServiceClient) q.uniqueResult();
log.debug(sc);

ServiceRequest sr = new ServiceRequest();
sr.setIpAddress(ipAddress);
sr.setUsername("test");
sr.setClient(sc);
sr.setCreateDate(java.util.Calendar.getInstance().getTime());

sess.save(sr);
sess.flush();


Full stack trace of any exception that occurs:
none

Name and version of the database you are using:
MySQL 3.23.x

The generated SQL (show_sql=true):
Code:
select servicecli0_.id as id, servicecli0_.name as name from client servicecli0_ where (servicecli0_.name=? )

select requests0_.client_id as client_id__, requests0_.id as id__, requests0_.id as id0_, requests0_.ip_address as ip_address0_, requests0_.username as username0_, requests0_.create_dt as create_dt0_, requests0_.mod_dt as mod_dt0_, requests0_.client_id as client_id0_ from request requests0_ where requests0_.client_id=?


Debug level Hibernate log excerpt:
This is the log before the ServiceRequests start being loaded
Code:
1875 DEBUG net.sf.hibernate.loader.Loader  - total objects hydrated: 1
1875 DEBUG net.sf.hibernate.impl.SessionImpl  - resolving associations for [testservice.ServiceClient#1]
1891 DEBUG net.sf.hibernate.impl.SessionImpl  - creating collection wrapper:[testservice.ServiceClient.requests#1]
1906 DEBUG net.sf.hibernate.impl.SessionImpl  - done materializing entity [testservice.ServiceClient#1]
1906 DEBUG net.sf.hibernate.impl.SessionImpl  - initializing non-lazy collections
1906 DEBUG net.sf.hibernate.impl.SessionImpl  - initializing collection [testservice.ServiceClient.requests#1]
1906 DEBUG net.sf.hibernate.impl.SessionImpl  - checking second-level cache
1906 DEBUG net.sf.hibernate.impl.SessionImpl  - collection not cached
1906 DEBUG net.sf.hibernate.impl.BatcherImpl  - about to open: 0 open PreparedStatements, 0 open ResultSets


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 12:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You are surely accessing the collection somewhere.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 12:16 pm 
Newbie

Joined: Wed Dec 22, 2004 11:36 am
Posts: 11
Everything I am doing is there in my message. I open the session and query for the object. ServiceClient doesn't initalize the request list on instantiation, only when addRequest or setRequests is called. I never access the list at all.

This is the setClient code (nothing special there)...
Code:
private ServiceClient client;
public ServiceClient getClient() { return client; }
public void setClient(ServiceClient client) { this.client = client; }


I tried getting the ServiceClient using Session.find, Query.list, and Query.uniqueResult - no change in behavior. It appears to think that the List is not lazy.


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.