Hi Will,
A simple approach is to use an S2 interceptor that opens the hibernate
session before calling the action and closes it afterwards.
This is the open-session-in-view pattern that's well described at the
hibernate site http://www.hibernate.org/43.html.
You don't need Spring or any other framework but you do need a way to
access the SessionFactory/EntityManager within the interceptor. I'm not
sure if you can use Guice to inject dependencies into interceptors. If
not you can pass the SessionFactory/EntityManager to your interceptor
through the application context that's accessible through the
ActionInvocation parameter.
Writing custom interceptors is described at
http://struts.apache.org/2.x/docs/writing-interceptors.html.
Hope that helps.
Jeromy Evans
Will Smith wrote:
Hi, I'm sure this has probably been beaten to death, but I can't seem to
find a viable solution without using spring.
I just switched over to using hibernate from ibatis (so much better now),
and I'm having trouble working my actions around hibernate's transaction
model. With ibatis you had to manually query for linked objects, e.x. a
customer has an address and both the customer and address have their own
table. In ibatis, I was manually wiring these things together with
multiple
queries. In hibernate, as long as the mapping is setup, it will wire
these
together for me. The problem I'm getting now is related to Lazy
loading (the
actual message says the the session has been closed).
As you've probably already figured out, I'm looking up an object from the
database in a method in my action, and calling an object that is a
member of
that object in my view. Hibernate tries to pull this from the
database, but
notices I already commited the session and gives an error. e.x. in a view
action i will do:
cust = hibernateSession.get(Customer.class, custId);
and in the view:
<s:property value="cust.address.street" />
The solutions I found to this problem when searching are related to using
openSessionInView as an interceptor, but this looks like it's tied to
using
Spring. Currently I'm using Guice, but if I must I will switch to Spring.
The other solution I found was a custom built interceptor that extended
AroundInterceptor which I found is deprecated.
What I'm trying to accomplish is opening the session before the action is
called, giving the action access to this session, and having the session
committed after the view is rendered.
Any help is greatly appreciated, and sorry for this being an off topic
post.
Thanks,
Will
------------------------------------------------------------------------
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.467 / Virus Database: 269.7.6/815 - Release Date: 22/05/2007 3:49 PM
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]