I am generating LazyInitializationException when viewing data loaded on a hibernate loaded domain object that has no relationships at all. I am using hibernate 3 with spring. When I insert the explicit lazy="false" as an attribute to the class this goes away, but I don't understand why I am getting it in the first place. It must be something dumb but I assume you shouldn't have to set the lazy=false just to make it work by default and from what i read this should only affect relationships. Maybe I am misunderstanding the documentation.
Essentially in this test case I am trying to load data into a simple address object and display the toString view of that address pojo.
Here is my class definition:
Code:
<class name="mbs.test_newmodel.domainobjects.entities.Address" table="ADDRESS">
<id name="id" column="ID" type="int" unsaved-value="-1" >
<generator class="identity"/>
</id>
<property name="line1" column="LINE1" not-null="true"/>
<property name="line2" column="LINE2"/>
<property name="type" column="TYPE" not-null="true"/>
<property name="city" column="CITY" not-null="true"/>
<property name="state" column="STATE" not-null="true"/>
<property name="postalCode" column="POSTAL_CODE" not-null="true"/>
<property name="country" column="COUNTRY" not-null="true"/>
<property name="companyId" column="COMPANY_ID" not-null="true"/>
</class>
Here is my code where the error occurs:
Code:
DemographicsMgr demographicsMgr = (DemographicsMgr) context.getBean("demographicsMgr");
Address address = demographicsMgr.getAddress(app.getUser().getDefaultCompanyId(), 1);
[b]System.err.println(address.toString());//**********ERROR OCCURS HERE[/b]
The getAddress method is implemented like so:
Code:
public Address getAddress(int companyId, int addressId)
{
return addressDao.get(companyId, addressId);
}
The implementation I am using in addressDao is as follows:
Code:
public Address get(int companyId, int addressId)
{
return (Address)getHibernateTemplate().load(Address.class, new Integer(addressId));
}
Full stack trace:
Code:
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 mbs.test_newmodel.domainobjects.entities.Address$$EnhancerByCGLIB$$eea99824.toString(<generated>)
at mbs.test_newmodel.service.entities.DefaultDemographicsMgr.main(DefaultDemographicsMgr.java:101)
And if you want to see it here is my spring application context used for this test:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Data source bean -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
<property name="url"><value>jdbc:hsqldb:hsql://localhost:9001</value></property>
<property name="username"><value>xxxxx</value></property>
<property name="password"><value>xxxxxx</value></property>
</bean>
<bean id="phoneDao" class="mbs.test_newmodel.dao.implementations.hibernate.enitities.PhoneDao">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="addressDao" class="mbs.test_newmodel.dao.implementations.hibernate.enitities.AddressDao_Hibernate">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="demographicsMgr" class="mbs.test_newmodel.service.entities.DefaultDemographicsMgr">
<property name="addressDao"><ref local="addressDao"/></property>
<property name="phoneDao"><ref local="phoneDao"/></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>test.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>