users@jersey.java.net

Re: [Jersey] Jersey + Spring + Hibernate = LazyInitializationException

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 05 May 2009 22:44:26 +0200

Hi Fábio,

Does the following help:

http://stackoverflow.com/questions/345705/hibernate-lazyinitializationexception-could-not-initialize-proxy

I think the problem is that after the method is returned the
transaction goes out of scope, but the object is lazily initialized
after the method has returned.

I often find that googling such exceptions can help:

http://www.google.co.uk/search?hl=en&q=org.hibernate.LazyInitializationException%3A+could+not+initialize+proxy+-+no+Session&btnG=Google+Search&meta=&aq=f&oq=

Paul.

On May 5, 2009, at 10:35 PM, Fabio Oliveira wrote:

> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>