-->
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.  [ 2 posts ] 
Author Message
 Post subject: Help understanding cause of LazyInitializationException
PostPosted: Fri Jan 14, 2011 12:35 am 
Newbie

Joined: Fri Jan 07, 2011 3:54 pm
Posts: 2
The method below results in the org.hibernate.LazyInitializationException being thrown and I'd appreciate help in understanding why. I'm using JPA 2/Hibernate & Spring.

JPA 2/Hibernate are using the default transaction persistence context, therefore, shouldn't the method below allow for lazy loading?

Note: daoWrapper is a convenience class that wraps entity manager methods; the class itself is also annotated with @Transactional.

Code:
    @Transactional(readOnly=true)
    public Response getGallery(@PathParam("id") int id) {
        Gallery g = daoWrapper.findById(Gallery.class, id);
        ...
        GalleryDto gDto = new GalleryDto();
        ...
        // getImages() returns a collection of 'image' objects.
        gDto.setImages(g.getImages());
        return Response.ok(gDto).build();
    }


Top
 Profile  
 
 Post subject: Re: Help understanding cause of LazyInitializationException
PostPosted: Fri Jan 14, 2011 5:53 am 
Beginner
Beginner

Joined: Tue Nov 02, 2010 4:29 am
Posts: 21
The collection is loaded lazily which means it is not populated.
Members can only be retrieved within the session that is scoped to the method transaction.
You are returning a lazily loaded collection which then tries to access the database outside of the session - cause the exception.
Either eagerly load the collection or trigger the retrieval on the collection prior to returning it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.