Oct 11th, 2007, 11:46 AM
Lazy Initialization Exception
I am having an issue when accessing a collection in my domain model. I basically have a domain object that has a number of one to many relationships. All of these relationships are bulk standard i.e.key/column associations, except one. The other relationship type is defined using a property-ref (I need to do this as we have composite-id's that dont match on the two database tables). I.e.
<set name="events" inverse="true" lazy="true">
<key property-ref="surrogateContractNo" column="CONTRACTNO"/>
Everything is wired up in Spring with a HibernateTransactionManger wrapping the session factory and a TransactionProxyFactoryBean wrapping the service that ultimately uses HibernateTemplate's to access data. Therefore the session should be open until a transaction completes.
All has been working well when issuing simple HQL to retreive data, however I have the need for a more complex query that for the time being I am using native SQL. This is defined as a named query in the mapping file. i.e.
<return alias="tr" class="Tranche"/>
from tranche tr
where 1 > (select count(*) from diary di
where di.contractno = tr.contractno
and di.d_type = 'ST'
and di.rel_day <= :relDay)
When I execute this query through the framework every appears to query back OK as per normal (correct result set size etc) but as soon as I access the collection shown above I get the following :
SEVERE: failed to lazily initialize a collection of role: com.xyz.domain.Tranche.events, no session or session was closed
11-Oct-2007 16:54:46 org.hibernate.LazyInitializationException
Anybody have any ideas as to why using the native SQL query would cause this issue to occur for the property-ref defined relationship ?