Ok so I have learned a bit more on transactions and session management so I have a bit more understanding now. However not enough to resolve my problem apparently.
I read in a Hibernate book about transaction management with lazy associations and thought I found the solution. Unfortunately though it has proved to be ineffective.
It told me to make me entire test method transactional, by annotating with:
@Transactional(readOnly=false)
Then I added this to my application context xml:
Code:
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
Its the same single sessionFactory I use on my DAO.
This has not made a difference when I run the code however, I get the same LazyInitializationException
So what did I miss here? Are there other steps involved to activate transactions which the book failed to mention or perhaps I misunderstood?
[Edit] Also I am noticing that the transactional part is not doing its job, like its not activated. In my code I first do an insert, and then afterwards I do the select which I then start reading objects which results in the LazyInitializationException. So if my transaction was valid, it would have automatically rolled back my insert. However I am seeing that despite the exception being thrown, the insert has been commited.