I have an application that uses Hibernate for persistence and am attempting to implement a JAX-WS interface (exposing it using the Spring Framework). I also have a service class (annotated with @WebSer开发者_如何学JAVAvice
and @WebMethod
) that calls methods in my persistence layer. I am getting exceptions (LazyInitializationException
) when working with lazy loaded objects.
I ran into the same problem with Quartz jobs and was able to resolve it by wrapping my job bean with a org.springframework.aop.framework.ProxyFactoryBean
and providing org.springframework.orm.hibernate3.HibernateInterceptor
. I attempted to wrap my service bean with the ProxyFactoryBean
, but received errors about the proxy not being an annotated web service.
Is there a way to do the same type of thing in this case, or what would be the best approach to managing my hibernate sessions to take in this scenario?
So, it looks like one potential option is to split my implementation into two classes. One that will contain all of the business logic that calls the database persistence layer, and another that exposes the web service and calls the business logic. The class containing the business logic can be wrapped with the Spring framework ProxyFactoryBean
to utilize a HibernateInterceptor
.
Java:
public class BusinessLogicClass implements MyInterface {
public void doBusinessLogic() {
//Perform Hibernate stuff here
}
}
@WebService
public class WebServiceClass {
BusinessLogicClass blc;
public setBlc(BusinessLogicClass blc) {
this.blc = blc;
}
@WebMethod
public void doLogic() {
blc.doBusinessLogic()
}
}
Spring XML Configuration:
<bean id="transactionalService" class="BusinessLogicClass">
</bean>
<bean id="transactionalServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="transactionalMessageQueueService"/>
</property>
<property name="proxyInterfaces">
<value>MyInterface</value>
</property>
<property name="interceptorNames">
<value>hibernateInterceptor</value>
</property>
</bean>
<bean id="webService" class="WebServiceClass">
<property name="blc">
<ref bean="transactionalServiceProxy"/>
</property>
</bean>
This does solve my problem. Though for my application it is a bit of annoyance to have two classes with basically the same methods just go get the persistence to work...
-- Update --
As an alternative (probably a better alternative) to using the proxy configuration in the Spring XML file annotation based translation management could be enabled and then each method in the BusinessLogicClass
that needed to be within a transaction could be annotated with @Transactional
. This is the approach I ultimately used.
精彩评论