Thomas Einwaller
Thomas Einwaller

Reputation: 9063

Hibernate session handling in spring web services

I am using spring-ws with Jaxb2Marshaller, PayloadRootAnnotationMethodEndpointMapping and GenericMarshallingMethodEndpointAdapter to configure my web services via the @Endpoint and @PayloadRoot annotations.

When I try to use the DAO's of my project I am able to load objects from the database but as soon as I try to access properties inside my service that should be lazily loaded I get a org.hibernate.LazyInitializationException - could not initialize proxy - no Session.

In my spring-mvc web application the OpenSessionInViewInterceptor handles the sessions. How do I configure my web service project to automatically create a Hibernate session for every web service call?

Upvotes: 1

Views: 6321

Answers (2)

Thomas Einwaller
Thomas Einwaller

Reputation: 9063

In the meanwhile I found a solution. This forum entry gave me the hint:

http://forum.springframework.org/showthread.php?t=50284

Basically I added the @Transactional annotations to my web service implementation class. The tricky part was to tell spring to use the original class (not the proxy created by tx:annotation-driven) which I achieved by using the following configuration:

<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping" >
    <property name="order" value="1" />
</bean>

<tx:annotation-driven mode="proxy" order="200" proxy-target-class="true" />

The order of the configuration statements seems important too.

Upvotes: 0

Simon Groenewolt
Simon Groenewolt

Reputation: 10665

Wrap a org.springframework.aop.framework.ProxyFactoryBean around the object in the spring context that needs the hibernate session to be present.

This article http://springtips.blogspot.com/2007/06/spring-and-hibernate.html shows how to do it.

If you experience problems because of lazy-loaded collections when using sessions this way there are at least 2 possible fixes:

Upvotes: 2

Related Questions