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