users@jersey.java.net

Jersey + Spring + Hibernate = LazyInitializationException

From: Fabio Oliveira <fabio.braga_at_gmail.com>
Date: Tue, 5 May 2009 17:35:55 -0300

Hi list!

Probably I'm doing something really stupid, but all day I'm fighting
against the integration between Spring and Jersey and Hibernate.

I'm trying to do the basic: to access some persistent classes using
a Restful architecture. But when I try to get a reference for another
persistent class, Hibernate throws a LazyInitializationException.

Example:

public class Person {
        private int id;
        private String name;
        private Address address;

        ... getters and setters ...
}

public class Address {
        private int id;
        private String street
        ... getters and setters ...
}

@Path("/person")
@Component
@Scope("singleton")
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Transactional
public class PersonResource {

        @Autowired
        private RepositorioPapel personRepository;

        @Path("{id}")
        @GET
        public Person getPerson(@PathParam("id")
        int id) {
        Person p = personRepository.get(id);
        if (p == null) {
            throw new NotFoundException("Person not found");
        }
        return p;
    }

}

My web.xml:

<web-app>
    <display-name>Server</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <filter>
        <filter-name>hibernateFilter</filter-name>

<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/resources/*</url-pattern>
    </filter-mapping>

    <listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Jersey Spring Web Application</servlet-name>

<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Spring Web Application</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

As you can see, I'm trying to use the OpenSessionInViewFilter, but
always with the same result:

org.hibernate.LazyInitializationException: could not initialize proxy -
no Session

org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)

org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)

org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
        Address$$EnhancerByCGLIB$$a03449c5.getStreet(&lt;generated&gt;)
        Address
$JaxbAccessorM_getStreet_setStreet_java_lang_String.get(MethodAccessor_Ref.java:52)
...


Anyone already did something similar? Any clues?

Thanks very much!


-- 
Fábio Braga de Oliveira
ArchitecTeam Consulting
http://www.linkedin.com/in/fabiobragaoliveira
E-mail: fabio.braga_at_gmail.com