PDA

View Full Version : LazyInitializationException in AbstractWizardFormController


egars
Dec 6th, 2006, 02:24 PM
Hi all,

I've put an AbstractWizardFormController together that allows users to fill a persistent object. I use annotations (Hibernate 3.2.x) and solved a lot of problems with annotations and the
org.springframework.orm.hibernate3.support.OpenSes sionInViewInterceptor thing configured in a SimpleUrlHandlerMapping.

The wizard controller's JSPs consist of a success, a failure and a series of four form pages.

The second of these pages uses a property of the command bean that is actually annotated with @Transient, as I don't want this property to be persisted as such. Instead, that property dispatches a getter-method to a more generic way of storing some data:

getTelephoneNumber()
is annotated with @Transient

and returns one item, selected from a Set<CommunicationMethod>.

However, this does not work, unless I annotate the getter method for the collection as follows:

@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
public Set<CommunicationMethod> getCommunicationMethods() {
.... etc....

Now, I see why it works as it does.

The EAGER annotation makes sure that all methods are fetched together with the object that is pointing at them.

My point is: why must I do this?
I would expect the interceptor to take care of this for me.
Instead, it throws a LazyIntializationException at me if I don't.

Why?

This is not so much of a problem that will keep me awake at night, but it does not seem to be very intuitive or logical.

Anyone?

Regards,

Egars

egars
Dec 6th, 2006, 02:28 PM
I forgot to point out that this is not a problem when the collection is used in the first form.

I expect that is has something to do with the fact that the object is fetched in formBackingObject, and that the object that is stored in the session is no longer connected tot he session anymore.

karldmoore
Dec 6th, 2006, 02:34 PM
I would presume this is due to the fact the OSIV will work within the JSP page (the view), but within the controller there is no session. If you load the collection within the page first, it doesn't need to go back to the database. Therefore within the controller its fine. Obviously if you mark the fetch mode as eager it works because its loaded straight away, meaning the controller doesn't have to go off to the database.

Give this a read.
http://www.jroller.com/page/kbaum/20050406