-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Detaching Objects and Transactions
PostPosted: Wed Dec 22, 2010 8:29 pm 
Regular
Regular

Joined: Tue Nov 30, 2004 4:23 pm
Posts: 62
I have a current issue with regards to an Object Model that I am using with an ORM model for persisting as well as annotated with JAXB to expose this DAO as a data service.

So I have a war -> data service (cxf - interface) -> data service impl -> DAO (JPA/Hibernate).

I am using Spring AOP and the transaction is a pointcut around the data service impl method.

I have a Person that has a Collection of Contacts and a Collection of Children, these associations are lazy loaded.

What appears to happen is that the Person is returned from JPA/Hibernate and then CXF is walking the object model to serialize it and it hits the lazy associations and then throws an exception because the transaction is closed and CXF/JAXB is forcing the retrieval of those lazy loaded entities.

Is there something I can do to resolve this or will I have to force detach these objects before serializing them with JAXB?

Thanks,

Jay

2010-12-22 16:01:09,248 DEBUG [http-8080-1] PhaseInterceptorChain - Invoking handleMessage on interceptor org.apache.cxf.interceptor.WrappedOutInterceptor@ec7d83: org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(Ph
aseInterceptorChain.java:241)
2010-12-22 16:01:09,248 DEBUG [http-8080-1] PhaseInterceptorChain - Invoking handleMessage on interceptor org.apache.cxf.interceptor.BareOutInterceptor@24617a: org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(Phase
InterceptorChain.java:241)
2010-12-22 16:01:09,310 ERROR [http-8080-1] LazyInitializationException - failed to lazily initialize a collection of role: com.foo.person.Person.contacts, no session or session was closed: org.hiber
nate.LazyInitializationException.<init>(LazyInitializationException.java:42)
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.foo.person.Person.contacts, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.iterator(Lister.java:274)
at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.iterator(Lister.java:261)
at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:129)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:152)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:322)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:681)
at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:150)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:322)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:681)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:151)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:185)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:305)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:312)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:71)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:490)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:328)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:257)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.writeObject(JAXBEncoderDecoder.java:532)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:222)
at org.apache.cxf.jaxb.io.DataWriterImpl.write(DataWriterImpl.java:169)
at org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor.writeParts(AbstractOutDatabindingInterceptor.java:111)
at org.apache.cxf.interceptor.BareOutInterceptor.handleMessage(BareOutInterceptor.java:68)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:244)
at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:78)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:244)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:110)
at org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:98)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:423)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:178)
at org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:142)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:103)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)
at java.lang.Thread.run(Thread.java:619)
2010-12-22 16:01:09,326 WARN [http-8080-1] PhaseInterceptorChain - Interceptor for {http://foo.com}PersonDataServiceEndpointService#{http://foo.com}getPersonById has throw
n exception, unwinding now: org.apache.cxf.common.logging.LogUtils.doLog(LogUtils.java:369)
org.apache.cxf.interceptor.Fault: Marshalling Error: failed to lazily initialize a collection of role: com.foo.person.Person.contacts, no session or session was closed
at org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:249)
at org.apache.cxf.jaxb.io.DataWriterImpl.write(DataWriterImpl.java:169)
at org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor.writeParts(AbstractOutDatabindingInterceptor.java:111)
at org.apache.cxf.interceptor.BareOutInterceptor.handleMessage(BareOutInterceptor.java:68)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:244)
at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:78)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:244)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:110)
at org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:98)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:423)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:178)
at org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:142)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:103)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.foo.person.Person.contacts, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.iterator(Lister.java:274)
at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.iterator(Lister.java:261)
at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:129)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:152)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:322)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:681)
at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:150)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:322)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:681)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:151)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:185)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:305)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:312)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:71)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:490)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:328)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:257)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.writeObject(JAXBEncoderDecoder.java:532)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:222)
... 27 more


Top
 Profile  
 
 Post subject: Re: Detaching Objects and Transactions
PostPosted: Thu Dec 23, 2010 6:06 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
What appears to happen is that the Person is returned from JPA/Hibernate and then CXF is walking the object model to serialize it and it hits the lazy associations and then throws an exception because the transaction is closed and CXF/JAXB is forcing the retrieval of those lazy loaded entities.

You said before "the transaction is pointcut around the data service", so why is it closed now? It must be around CXF processing.

Quote:
Is there something I can do to resolve this or will I have to force detach these objects before serializing them with JAXB?

Kind of the opposite, you must guarantee that the Session is open until you finished serializing, you should couple the Session lifecycle to the transaction, i.e. also around the CXF processing.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Detaching Objects and Transactions
PostPosted: Thu Dec 23, 2010 12:33 pm 
Regular
Regular

Joined: Tue Nov 30, 2004 4:23 pm
Posts: 62
Thanks for your response.

To add some clarity, I am using the Lazy Loading as a way of providing one service method for getting a Person and then using the Fetch for retrieving a more fine or coarse grained object.

So I don't want the JAXB binding to cause the associations to be fetched, unless I intentionally call the Fetch/Join.

In order to do this, will I need to call detach so that when JAXB attempts to serialize the object, the collection sizes are causing a fetch of the lazy loaded associations to occur?

Thanks for your help,

jay


Top
 Profile  
 
 Post subject: Re: Detaching Objects and Transactions
PostPosted: Thu Dec 23, 2010 12:47 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
even if the object is detached, it's illegal (throws exception) to try accessing a non-loaded property: proxies representing your non-initialized collections will do that.
Detaching the object won't help in limiting the scope of serialization; if you want to show a different model you have to copy the values to a different type or to an unmanaged instance.
Alternatively you can play with "@ExtraLazy" options to minimize loading on collection access, set the Session in read-only mode, and clear() all collections. I wouldn't recommend this as your code will end up to be tightly closed to your model and you will need to update this logic when you add new properties/collections.

It would be better to instruct JAXB to which depth it has to go from the root entity, if that's possible as I don't use JAXB that much; it's definitely possible with other tools such as Gilead.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Detaching Objects and Transactions
PostPosted: Thu Dec 23, 2010 12:55 pm 
Regular
Regular

Joined: Tue Nov 30, 2004 4:23 pm
Posts: 62
Thanks for your response. I found the following post and it explains the exact same problem I am having and provides the solution you proposed.

Unfortunately, I essentially will have to do a clone of the object to remove any references to proxied associations.

http://www.mojavelinux.com/blog/archive ... f_my_pojo/

Thanks for your response,

Jay


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.