users@jersey.java.net

Re: [Jersey] Problem marshalling JPA entity

From: Comerford, Sean <Sean.Comerford_at_espn3.com>
Date: Thu, 1 Oct 2009 14:07:08 +0000

Paul -

You were correct in that was related to lazy fetch. I missed we were using
lazy on season - guess I should have looked more closely at the stack trace!

Thanks for debugging my code ;-)

A related question though: is there an example of how you could leverage
container managed persistence to have the jersey servlet somehow inject an
entity manager in your resource?

Ps - I can't say enough how slick Jersey + JAXB is. Very cool stuff.

On 9/30/09 5:26 AM, "Paul Sandoz" <Paul.Sandoz_at_Sun.COM> wrote:

> Hi Sean,
>
> How are you obtaining the entityManagerFactory?
>
> The error is occurring when the game instance is being marshaled by
> JAXB. Notice that lazy initialization is being utilized by hibernate.
> Thus i am wondering that by the time marshaling is being performed the
> persistent session has been closed.
>
> Is entityManagerFactory and entityManager fields of the resource
> class? Are you performing any clean up in the resource class related
> to the entityManager?
>
> Can you try quick experiment and attempt to directly invoke the method
> Season.getEndDate (i am guessing this from the stack trace). And/or
> alternatively directly marshall out the game instance from within the
> getGame method.
>
> Paul.
>
> On Sep 29, 2009, at 8:39 PM, Comerford, Sean wrote:
>
>> So Iıve a JPA entity BasketballGame that has a ManyToOne
>> relationship with Season.
>>
>> I am able to successfully load a given game from the DB using this
>> in both a Java main and Java servlet.
>>
>> But if stick the exact same code in a Jersey method, it throws the
>> following exception:
>>
>> javax.persistence.EntityNotFoundException: Unable to find
>> com.espn.sports.persistence.Season with id
>> com.espn.sports.persistence.Season$Key_at_7db
>>
>>
>> Again, exact same code in a standard servlet works fine but throws
>> the exception when run via the Jersey servlet.
>>
>> Any guesses as to what could be up?
>>
>> FYI, the code looks like this.... Full stack trace at end but
>> doesnıt seem that helpful to me:
>>
>> @GET
>> @Path("/{gameId}")
>> @Produces({"text/xml", "application/xml", "application/json"})
>> public BasketballGame getGame(@PathParam("gameId") int gameId) {
>> Long id = Long.valueOf(gameId);
>>
>> entityManager = entityManagerFactory.createEntityManager();
>> BasketballGame game =
>> entityManager.find(BasketballGame.class, id);
>>
>> if (game == null) {
>> throw new NotFoundException("Error - no game found w/ id
>> " + id);
>> }
>>
>> return game;
>> }
>>
>> javax.persistence.EntityNotFoundException: Unable to find
>> com.espn.sports.persistence.Season with id
>> com.espn.sports.persistence.Season$Key_at_7db
>> at org.hibernate.ejb.Ejb3Configuration
>> $
>> Ejb3EntityNotFoundDelegate
>> .handleEntityNotFound(Ejb3Configuration.java:113)
>> at
>> org
>> .hibernate
>> .proxy
>> .AbstractLazyInitializer
>> .checkTargetState(AbstractLazyInitializer.java:154)
>> at
>> org
>> .hibernate
>> .proxy
>> .AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:143)
>> at
>> org
>> .hibernate
>> .proxy
>> .AbstractLazyInitializer
>> .getImplementation(AbstractLazyInitializer.java:174)
>> at
>> org
>> .hibernate
>> .proxy
>> .pojo
>> .javassist
>> .JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
>> at com.espn.sports.persistence.Season_$
>> $_javassist_355.getEndDate(Season_$$_javassist_355.java)
>> at com.espn.sports.persistence.Season
>> $
>> JaxbAccessorM_getEndDate_setEndDate_java_util_Date
>> .get(MethodAccessor_Ref.java:52)
>> at
>> com
>> .sun.xml.bind.v2.runtime.reflect.Accessor.getUnadapted(Accessor.java:
>> 147)
>> at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor
>> $CompositeTransducedAccessorImpl.hasValue(TransducedAccessor.java:245)
>> at
>> com
>> .sun
>> .xml
>> .bind
>> .v2
>> .runtime
>> .property
>> .SingleElementLeafProperty
>> .serializeBody(SingleElementLeafProperty.java:96)
>> 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.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:
>> 319)
>> at
>> com
>> .sun
>> .xml
>> .bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:
>> 589)
>> at
>> com
>> .sun
>> .xml
>> .bind
>> .v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:
>> 312)
>> 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
>> com
>> .sun
>> .jersey
>> .core
>> .provider
>> .jaxb
>> .AbstractRootElementProvider
>> .writeTo(AbstractRootElementProvider.java:167)
>> at
>> com
>> .sun
>> .jersey
>> .core
>> .provider
>> .jaxb
>> .AbstractRootElementProvider
>> .writeTo(AbstractRootElementProvider.java:143)
>> at
>> com
>> .sun
>> .jersey.spi.container.ContainerResponse.write(ContainerResponse.java:
>> 254)
>> at
>> com
>> .sun
>> .jersey
>> .server
>> .impl
>> .application
>> .WebApplicationImpl._handleRequest(WebApplicationImpl.java:724)
>> at
>> com
>> .sun
>> .jersey
>> .server
>> .impl
>> .application
>> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:647)
>> at
>> com
>> .sun
>> .jersey
>> .server
>> .impl
>> .application
>> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:638)
>> at
>> com
>> .sun
>> .jersey.spi.container.servlet.WebComponent.service(WebComponent.java:
>> 309)
>> at
>> com
>> .sun
>> .jersey
>> .spi
>> .container.servlet.ServletContainer.service(ServletContainer.java:425)
>> at
>> com
>> .sun
>> .jersey
>> .spi
>> .container.servlet.ServletContainer.service(ServletContainer.java:590)
>> at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
>> at
>> org
>> .apache
>> .catalina
>> .core
>> .ApplicationFilterChain.servletService(ApplicationFilterChain.java:
>> 427)
>> at
>> org
>> .apache
>> .catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
>> 315)
>> at
>> org
>> .apache
>> .catalina
>> .core.StandardContextValve.invokeInternal(StandardContextValve.java:
>> 287)
>> at
>> org
>> .apache
>> .catalina.core.StandardContextValve.invoke(StandardContextValve.java:
>> 218)
>> at
>> org
>> .apache
>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
>> at
>> org
>> .apache
>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
>> at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
>> at
>> com
>> .sun
>> .enterprise
>> .web
>> .PESessionLockingStandardPipeline
>> .invoke(PESessionLockingStandardPipeline.java:98)
>> at
>> org
>> .apache
>> .catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
>> at
>> org
>> .apache
>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
>> at
>> org
>> .apache
>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
>> at
>> org
>> .apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:
>> 587)
>> at
>> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1093)
>> at
>> org
>> .apache
>> .catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
>> 166)
>> at
>> org
>> .apache
>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
>> at
>> org
>> .apache
>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
>> at
>> org
>> .apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:
>> 587)
>> at
>> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1093)
>> at
>> org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:
>> 291)
>> at
>> com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter
>> (DefaultProcessorTask.java:666)
>> at
>> com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess
>> (DefaultProcessorTask.java:597)
>> at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process
>> (DefaultProcessorTask.java:872)
>> at
>> com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask
>> (DefaultReadTask.java:341)
>> at
>> com
>> .sun
>> .enterprise
>> .web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:
>> 263)
>> at
>> com
>> .sun
>> .enterprise
>> .web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:
>> 214)
>> at com.sun.enterprise.web.portunif.PortUnificationPipeline
>> $PUTask.doTask(PortUnificationPipeline.java:382)
>> at
>> com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:
>> 264)
>> at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run
>> (SSLWorkerThread.java:106)
>> |#]
>>
>> --
>> Sean Comerford, Software Engineer
>> ESPN.com Site Architecture Group
>> Office: 860.766.6454 Cell: 860.329.5842
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
Sean Comerford, Software Engineer
ESPN.com Site Architecture Group
Office: 860.766.6454    Cell: 860.329.5842