views:

2511

answers:

2

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?

+1  A: 

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:

  • Add a Hibernate.initialize() call to the collection in code that is executed with the Hibernate session available - http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Hibernate.html#initialize(java.lang.Object)
  • Use a non-lazy collection by adding lazy="false" to the mapping - watch out when using this option, you can easily force hibernate to load your whole database with a couple of badly placed lazy="false" options.
Simon Groenewolt
I found out that the DAO works but lazy loading fails ... the tip in the link does not help me
Thomas Einwaller
Added 2 possible fixes for this to my answer.
Simon Groenewolt
A: 

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.

Thomas Einwaller