0

I have already implemented java web application (core application) which gives ability to plug modules into the core application. I'm getting org.hibernate.LazyInitializationException could not initialize proxy - no Session exception while trying to iterate through the properties of an java object with the help of reflection.

I found that, I can solve this by enabling eager loading with lazy="false" attribute for hibernate mapping file. But I don't want to do that, since hibernate mapping files are inside my core code.

And I tried the solution in this, but it didn't help me either. I got the same error.

Is thre any other ways to solve this issue at the runtime ? Appreciate your valuable thoughts.

Community
  • 1
  • 1

4 Answers4

0

I think you may be trying to call the object method outside a transaction. To lazy load the objects, the call on the proxy should be inserted in a transaction.

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
  • 1
    Yes, I'm not using this object inside a transaction. Can that be the reason ? If yes, do you have any idea of how should I address this ? – user2782789 Sep 16 '13 at 06:46
0

You need to implement what is popularly known as OpenSessionInView pattern.

It's implemented through the use of a Sevlet Filter and how you configure it depends on whether you're using other frameworks like Spring in your web layer or not.

A typical implementation makes use of Spring's OpenSessionInViewFilter like

<filter>
    <filter-name>SpringOpenSessionInViewFilter</filter-name>
    <filter-class>
       org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>SpringOpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

This has to come before any other Filters configured in your web.xml. If you're using Spring to build the session factory for you the Filter needs to be configured with

<init-param>
    <param-name>sessionFactoryBeanName</param-name>
    <param-value>sessionFactory</param-value>         
</init-param> 
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

This error comes when you tried to access the object which are not bound to the current session of hibernate.

You can load lazy initialized data like this :

Hibernate.initialize(YOUR_OBJECT.GETTER_METHOD_OF_YOUR_LAZY_LOADDED_ATTRIBUTE)

You can initialize it before transaction gets closed.

Naresh J
  • 2,087
  • 5
  • 26
  • 39
0

You are using the object outside the transaction.

Of course, as Naresh suggested, you can force the fetching of lazy fields before using your object outside the transaction by using Hibernate.initialize(lazyObject) and it should work. Using a native query, HQL, Hibernate Worker to fetch all your needed fields before access outside the transaction should work too and may be a better solution than using Hibernate.initialize(): you can write a better query to your database (better performance).

OpenSessionInView enables you to access the session from your view: accessing lazy fields with lazy-loading enabled will work with this solution. However you should consider all the effects of enabling access of the session from the view, some consider this a bad practice as you should know which fields you need before manipulating them in your view: it is not your view's role to initiate queries. But still it can be a good "workaround": choose carefully according to your needs.

zenbeni
  • 7,019
  • 3
  • 29
  • 60