Page 1 of 1

org.hibernate.LazyInitializationException

Posted: Thu Aug 23, 2012 10:06 am
by math7189
Hi,

I'm still working on adding RPC on the demo site and still have troubles. I managed to add Broadleaf services, such as CatalogServices. I can call methodes of this service, but there is a problem when returning the result to RPC.

I got the org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.broadleafcommerce.core.catalog.domain.SkuImpl.skuMedia no session or session was closes.

I googled this error, and it's because the hibernate session has closed, or after the object has been detached from the session. They suggested to set the key lazy="false" in the hibernate mapping file. I'd like to know which files I have to modify and where they are stored.

Thanks

Re: org.hibernate.LazyInitializationException

Posted: Thu Aug 23, 2012 3:08 pm
by aazzolini
Make sure that your service method is tagged with

@Transactional("blTransactionManager")

if you're using our transaction manager outside of a session. (In a session, the OpenSessionInViewFilter will wrap the entire session in a transaction to prevent lazy inits)

Re: org.hibernate.LazyInitializationException

Posted: Mon Aug 27, 2012 8:57 am
by math7189
I tried to tag it with the annotation but doesn't work. Do I need to add something in the context application file?

Re: org.hibernate.LazyInitializationException

Posted: Mon Aug 27, 2012 9:56 am
by aazzolini
You don't necessarily need anything specifically in your applicationContext.xml, but the class does need to be component scanned by Spring for the @Transactional annotation to work.

If you'd rather not scan the class, you could use the following bit of XML to achieve exactly the same thing the annotation does:

Code: Select all

<aop:config>
   <aop:pointcut id="yourServiceOperation" expression="execution(* org.broadleafcommerce.core.order.service.OrderService.*(..))"/>
   <aop:advisor advice-ref="blTxAdvice" pointcut-ref="yourServiceOperation" order="1"/>
</aop:config>


If you do it this way, you may want to specify only the methods that you need the transactional wrapper around as AOP does add some performance overhead. You would do this by instead of specifying "OrderService.*" only "OrderService.myMethod" for example.