Lazy Load Execption with Seam managed persistence context, not so with container managed pc
coresystems_rit Jun 10, 2009 5:34 PMHi
I've experience a somehow strange situation with two beans and a lazy load exception when using seam managed persistence context.
Starting Situation:
- (bean1) SFSB with scope SESSION and a collection annotated as a data model (@DataModel). Additionally there is a method which loads the data from the database into the collection. This method is also marked with @Factory as well as @Observer(some.event
).
- (bean2) SFSB with scope CONVERSATION. In one of those method the some.event
gets fired.
The bean1 contains a list of event entities (e.g. party events or so). One has the possibility to edit such an event. In this case, a method on bean2 gets called. This method is annotated with @Begin. After doing the modifications on the event, the users clicks the save
button. This button calls the commit() method on the bean2 (bean2.commit()) which is annotated with @End(beforeRedirect eq true). If the modifications could be done, the bean2 fires an event some.event
. This event is observed by bean1 with @Observer(some.event
). In the method with the @Observer annotation, the bean1 updates the collection behind the data model to reflect the changes done during the previous conversation (related to bean2).
Problem:
If I am using a seam managed persistence context I am getting a lazy loading exception. As far as I understand, the situation of the method calls is as follow:
1) user clicks save
- bean2.commit();
2) bean2.commit(): Events.instance.raiseEvent(some.event
)
3) directly after the event all observers get notified (before the commit method finishes) - bean1.observeMethod catches
4) bean1.observeMethod reloads data from the database
5) bean1.observeMethod is done, jumps now back to bean2.commit
6) bean2.commit finishes the remaining code
7) jsf starts rerendering user interface (- access the collection)
As the entity manager used in step 4 to load the data from the database isn't available any more in step 7 (because it belonged to bean2. but the conversation in bean2 already ended and therefor the pc is destroyed), i get a lazy loading exception. If I change from @In (or better say from seam managed persistence context to a container managed one (@PersistenceContext(type eq EXTENDED)), i didn't get such a lazy load exception. Unfortunately the container managed persistence context doesn't reload the data from the database).
My question is, how can I stick a seam managed persistence context to a session? And how can I achieve that the bean1 uses his own entity manager all the times?
Thanks for your help!
Thierry