users@jersey.java.net

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

From: Fabio Oliveira <fabio.braga_at_gmail.com>
Date: Wed, 6 May 2009 15:33:56 -0300

Looking at the stack trace, it is like the error happens while the
jaxb tries to get a lazy property.... What I doesn't understand is why
this happen, if I'm using the OpenSessionInViewFilter and I'm tagging
the resource method with the @Transaction...

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)
        br.com.simula.domain.base.Papel$$EnhancerByCGLIB$$35cb81c3.getDescricao(&lt;generated&gt;)
        br.com.simula.domain.base.Papel$JaxbAccessorM_getDescricao_setDescricao_java_lang_String.get(MethodAccessor_Ref.java:52)
        com.sun.xml.bind.v2.runtime.reflect.Accessor.getUnadapted(Accessor.java:147)
        com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.hasValue(TransducedAccessor.java:245)
        com.sun.xml.bind.v2.runtime.property.SingleElementLeafProperty.serializeBody(SingleElementLeafProperty.java:96)
        com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:332)
        com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:698)
        com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:152)
        com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:332)
        com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:592)
        com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:320)
        com.sun.xml.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBody(ArrayReferenceNodeProperty.java:114)
        com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:152)
        com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:332)
        com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:328)
        com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:592)
        com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:320)
        com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:493)
        com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:325)
        com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:254)
        javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
        com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:167)
        com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:143)
        com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:254)
        com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:693)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:616)
        com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:607)
        com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:309)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:425)
        com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:590)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
        org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

On Wed, May 6, 2009 at 1:28 PM, Rabick, Mark A (IS) <Mark.Rabick_at_ngc.com> wrote:
> My experience is with EJB3/JPA/Hibernate on Weblogic Server 10.1 and 10.3...  I noticed earlier in this chain that you were manually handling transactions (begin/commit) I think.  If Spring supports container-managed transactions (CMT), the begin/end is handled by the transaction and in our implementation, the session doesn't close until all pending 'operations' on the persistence layer are complete (ie. Lazy inits)
>
> Not sure this is exactly your problem.
>
> -mark
>
> _______________________________________________
> Mark A. Rabick - Software Engineer
> Em: mark.rabick_at_ngc.com
>
>
>
>> -----Original Message-----
>> From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
>> Sent: Wednesday, May 06, 2009 11:12 AM
>> To: users_at_jersey.dev.java.net
>> Subject: Re: [Jersey] Jersey + Spring + Hibernate =
>> LazyInitializationException
>>
>>
>> On May 6, 2009, at 4:02 PM, Fabio Oliveira wrote:
>>
>> > Hi, Paul, thanks for your answer.
>> >
>> > But in my case I'm using the
>> >
>> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter in
>> > my web.xml configuration, this isn't enough?
>> >
>> > Are you doing something alike? Could you show some examples?
>> >
>>
>> I am not, i do not have any experience using Hibernate and Spring.
>>
>> Perhaps asking on a Spring-related forum might help? since i
>> am guessing this issue is not specific to Jersey+Spring.
>>
>> Paul.
>>
>> > Thanks again!
>> >
>> > On Tue, May 5, 2009 at 5:44 PM, Paul Sandoz <Paul.Sandoz_at_sun.com>
>> > wrote:
>> >> Hi Fábio,
>> >>
>> >> Does the following help:
>> >>
>> >>
>> http://stackoverflow.com/questions/345705/hibernate-lazyinitializatio
>> >> nexception-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.LazyInitializati
>> >>
>> onException%3A+could+not+initialize+proxy+-+no+Session&btnG=Google+Se
>> >> arch&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.ContextLoaderListene
>> >>> r
>> >>> </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
>> >>>
>> >>
>> >>
>> >>
>> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> >> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>> >>
>> >>
>> >
>> >
>> >
>> > --
>> >
>> > Fábio Braga de Oliveira
>> > ArchitecTeam Consulting
>> > http://www.linkedin.com/in/fabiobragaoliveira
>> > E-mail: fabio.braga_at_gmail.com
>> > Mobile: +55 19 9270-6574
>> >
>> >
>> ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
>> >
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>



-- 
Fábio Braga de Oliveira
ArchitecTeam Consulting
http://www.linkedin.com/in/fabiobragaoliveira
E-mail: fabio.braga_at_gmail.com
Mobile: +55 19 9270-6574