Aug 1st, 2009, 10:30 AM
Anybody knows how to config OpenSessionInViewInterceptor in AOP?
I've searched this forum and haven't found any topics regarding this issue. So I post here a new thread and hope someone with that experience could help me.
I am currently working on a project and I use Spring aop and Spring hibernate transacation manager to manage hibernate session.
The service layer is out of the bound of Hibernate session(I thought it's fine because Service layer wouldn't need to utilize Hibernate Session anyway, it's the job of Dao layer). But when I tried to access the data across tables with references. Eg, one to many relationships. Hibernate pops up that error says the lazy load inialization failed, because the method that where I tried to access data is out of Hibernate session(in Service Layer). So if I want to take advantage of hibernate's lazy load, then I have to make sure that any transactions that are made is within Hibernate Session's scope.
There is an alternative solution is that I could manually load this data as I need by setting Fetchmode to be Fetchmode.Join, which basically is like manually override lazy load and loads all data that are referenced to the current table. The drawback is everytime you have to add these lines of code and make your code look messy.
I goolgled on the internet and found that the openSessionInViewInterceptor seems like the best solution to this issue. I tried to use Spring AOP to set a cut point and let the interceptor keeps the session active in my service layer.
Anybody can give me an example of how to config it using Spring AOP ???