Hibernate version: 2.1.6
Name and version of the database you are using: Oracle 9i
Hi folks,
I've resently started using the "long sessions" pattern (Hibernate in Action - page 326) in my servlets. I'm using the HibernateUtil class and have implemented the servlet filter as described. All is well! However, I have a slight issue.
I use an Interceptor to create history for object changes (a story in itself). This Interceptor has to know about the "owner" of the changes (person making the the changes to the object). Previously I was using detached objects so it was easy just to pass in the owner as part of the Interceptor's constructor everytime I created a new session.
EX:
public class HistoryInterceptor implements Interceptor, Serializable { private HistoryWriter history; private boolean isHistoryOn = false; public HistoryInterceptor(Operator admin) { super(); history = new HistoryWriter(admin); isHistoryOn = true; } .........
Now, however, I have these long sessions that are created on the fly in the servlet filter and don't have an easy way to pass in my owner to the interceptor. So I tried the following, but it seems that the instance of the Interceptor changes from my orginal instance.
In the servlet filter I do:
HibernateUtil.registerInterceptor(new HistoryInterceptor());
Then, when I'm about to save an object I make a call like this:
((HistoryInterceptor) HibernateUtil.getInterceptor()).setOwner(admin);
However the interceptor instance seems to change between when I make the call to setOwner and when the interceptor actually get's called by Hibernate. If someone see's a reason why this wouldn't work, could you point it out? Any feedback is very much appreciated!
|