-->
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.  [ 3 posts ] 
Author Message
 Post subject: LazyInitializationException and @Transactional
PostPosted: Fri Jun 20, 2014 3:10 am 
Newbie

Joined: Fri Jun 20, 2014 2:45 am
Posts: 4
With commented @Transactional I get LazyInitializationException, when I uncommnet @Transactional test works fine.
Why @Transactional is not needed to execute query and to access Order object, but is needed to access order items ?

the outer loop for (Order order : result) works fine,
exception is in inner for (Item item:order.getItems())

Why ?

Code:
   @Test
//   @Transactional
   public void testSimpleCriteria() {
      CriteriaBuilder builder = entityManager.getCriteriaBuilder();
      
      CriteriaQuery<Order> criteria = builder.createQuery(Order.class);
      Root<Order> orderRoot = criteria.from(Order.class);
      criteria.select(orderRoot);
      criteria.where(builder.like(orderRoot.<String>get("customer"), "J%"));      
      
      TypedQuery<Order> typedQuery = entityManager.createQuery(criteria);
      
      List<Order> result= typedQuery.getResultList();
      
      for (Order order : result) { // here is no exception, order is fetched correctly !
         for (Item item:order.getItems()) { // exception appears only here !!
            System.out.println("item: " + item);
         }
      }
   }


top of stack trace is
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.javatech.app.Order.items, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)


I suppose, that if it where in web app, than the same exception would appear in the same place, and if I would use OpenSessionInViewFilter the code would work fine.
So does OpenSessionInViewFilter do the same as @Transactional ?


Top
 Profile  
 
 Post subject: Re: LazyInitializationException and @Transactional
PostPosted: Fri Jun 20, 2014 6:45 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,
I'm not familiar with Spring but the problem seems to be that the session is closed when you call order.getItems(). Unless you fetch the items eagerly, Hibernate will execute a separate query to get them.

If you add @Transactional, I guess the whole test is executed inside a transaction and the session is opened and closed at the end of it. Meaning that the query to get the items can be executed.

When you remove the @Transactional the entityManager is not inside a transaction when it is trying to get the items and therefore you have a lazy initialization exception.

The OpenSessionInViewFilter should work since it keeps the session open for the entire duration of the web request. If you search for "Open View Pattern" you will find plenty of explanations for it.


Top
 Profile  
 
 Post subject: Re: LazyInitializationException and @Transactional
PostPosted: Fri Jun 20, 2014 7:12 am 
Newbie

Joined: Fri Jun 20, 2014 2:45 am
Posts: 4
Thx for Your reply,

But why
List<Order> result= typedQuery.getResultList();
executes successfully even without @Transactional ?
Is transaction opened implicitly by entityManger at start of getResultList and closed at end of getResultList ?

Is database session the same as database transaction ?


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