Page 1 of 1

LazyInitializationException - OrderService

Posted: Wed Nov 26, 2014 12:44 pm
by gowthamgutha
I have a custom class in my own package in the site module. It is a @Service class and I have put @Transactional("blTransactionManager") to both the class as well as the method in which I have called the orderService.save() method.

Code: Select all

       @Transactional("blTransactionManager")
   private void changeOrderStatus(Order order,OrderService orderService)
   {
      order.setStatus(OrderStatus.SUBMITTED);

      try {
         orderService.save(order, true);
         System.out.println("order saved");
      } catch (PricingException e) {
         System.out.println("Exception occured while saving order");
         e.printStackTrace();
      }
   }


Also I found that when I am trying to inject OrderService into the class, it isn't being injected. I added the package of the class in core module applicationContext.xml file using <context:component-scan>

This is the error I am getting

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.broadleafcommerce.core.order.domain.OrderImpl.addedOfferCodes, could not initialize proxy - no Session

Re: LazyInitializationException - OrderService

Posted: Tue Dec 02, 2014 8:03 pm
by phillipuniverse
First of all, no need to put @Transactional at the class level.

Second of all, your first problem is that OrderService is not being injected into the class. You will definitely need to solve that one first.

Third of all, orderService.save() already appropriately manages transaction boundaries so you do not need to wrap your changeOrderStatus method in a @Transactional.

How are you invoking changeOrderStatus? I would be willing to bet that the Order instance that you are passing to that method is somehow using an Order that is detached from the Hibernate session.