I am trying to use hibernat template to execute the following code
Code:
HibernateFactory.buildSessionFactory();
sessionFactory = HibernateFactory.getSessionFactory();
template = new HibernateTemplate(sessionFactory);
template.saveOrUpdate(event);
Event obj = (Event) template.load(Event.class, event.getId());
assertEquals("Loads the event", event.getName(), obj.getName());
When the following line
Code:
obj.getName()
is executed I get the following error.
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
My config files are as follows:
Location.hbm.xml
Code:
<hibernate-mapping package="com.manning.hq.ch07">
<class name="Location" table="locations">
<id name="id" type="long" >
<generator class="native"/>
</id>
<property name="name" type="string"/>
</class>
</hibernate-mapping>
Event.hbm.xml
Code:
<hibernate-mapping package="com.manning.hq.ch07">
<class name="Event" table="events">
<id name="id" type="long" >
<generator class="native"/>
</id>
<property name="name" type="string"/>
<property name="startDate" column="start_date" type="date"/>
<property name="duration" type="integer"/>
</class>
</hibernate-mapping>
By any chance is the following line
Code:
template.saveOrUpdate(event);
closing the session. If you look at the config files, i have not defined any association, so lazy loading is not in picture. I am not able to understand why i get this error.
Can some help to understand the concept