readthefuckingmanual.net

[SOLVED] org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Error added: 2007-10-27T19:23:07Z

6 people waiting for the answer...

24 answers found.

Answer 1124 (96.15385% helpful)

use OpenSessionInViewFilter/Interceptor or the HibernateInterceptor to keep a Hibernate session open during a request
Permalink

Answer 1258 (71.42857% helpful)

Please notice the difference between the load() and get() method. The load method ( you are using probably ) may return a proxy, so you cannot access to the nested properties. 
Permalink

Answer 1378 (77.27273% helpful)

LazyLoad = false <- Worst practice ever
Get or Load with Spring <- Bad practice
Solution: 
Use the OpenSessionInViewFilter as said before.
OR
Use criteria.fetchMode/hsql fetch within the session you\'re using.
Permalink

Answer 1372 (66.666664% helpful)

 <class name=\"com.cap.hibernate.Employee\" table=\"Employee\" lazy=\"false\">
Permalink

Answer 594 (66.666664% helpful)

There an be two solutions:

1] whichever object creating this problem,Use HibernateTemplate.initialize(object name)

2]Use lazy=false in your hbm files
Permalink

Answer 888 (70.588234% helpful)

If using hibernateTemplate, this can be avoided by using get instead of load
Permalink

Answer 952 (66.666664% helpful)

@ManyToMany(fetch=FetchType.EAGER) if you are using anotation onetoone onetomany write  fethch type default lazy=true. It gives the same result given solutations above.
Another point check your

 <prop key=\"hibernate.max_fetch_depth\">5</prop>
	            <prop key=\"hibernate.default_batch_fetch_size\">16</prop>
	            <prop key=\"hibernate.jdbc.batch_size\">25</prop>
	            <prop key=\"hibernate.jdbc.fetch_size\">8</prop> 


parameters in your mapping files. But pay attention if you give big numbers this will make a bad affect in your project performance.
Permalink

Answer 875 (63.636364% helpful)

what the hell solution, this all peoples provide
Permalink

Answer 1659 (55.555553% helpful)

Hi,
Check Below URL for the solution of this problem:

<a href="http://shivasoft.in/blog/java/hibernate/cause-and-solution-of-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session-error/">click here to get solution</a>

http://shivasoft.in/blog/java/hibernate/cause-and-solution-of-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session-error/


Regards,
Jitendra Zaa
Permalink

Answer 1376 (50.0% helpful)

Answer 19 works for me, try this in your web.xml
	<!-- Include this if you are using Hibernate -->
    <filter>
        <filter-name>Spring OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
Permalink

Answer 957 (50.0% helpful)

Adding lazy=\"false\" will solve the problem, but as it will lead to loading of collection sat the startup,the appplication will be slow
Permalink

Answer 1010 (50.0% helpful)

use lazy= false in hibernate mapping file.
check :
http://java-masters.blogspot.com
Permalink

Answer 1434 (50.0% helpful)

Answer 16!!!

I have found a session.clear() in my code. After that call no properties could be loaded lasily any more.
Permalink

Answer 653 (42.857143% helpful)

use org.hibernate.Hibernate.intialize();
Permalink

Answer 965 (0.0% helpful)

Hej, I had this problem and I think I solved it, or at least identified some kind of problem with my code. My idea was something like this: 

	public static Object getData(Object object, Long id){
		Object obj = new Object();
		Session session = HibernateUtil.getSessionFactory().openSession();		
		
		if(object instanceof Incident){
			 obj = (Incident)(session.load(Incident.class, id));
			System.out.println(\"Innuti objec instanceof incident\");
			
		}
		
		System.out.println(\"Komej in till Innuti objec instanceof incident\");
		session.close();
		HibernateUtil.shutdown();
		
		return obj;
		
		
	}

The Incident was going to be returned and used while continuing updating the data before a session.flush in the calling method. It crashes when trying to close the session. I am not sure why, but the effect it would have if it was possible, I assume, is that it wouldn\'t be possible to use the incident object since it is retreiving the data from the database while using it. I think...! What I cannot understand is why the error isn\'t thrown when trying to access the data. Perhaps the application is smart enought to predict the usage, or I have multiple errors...

Hope it helps, and if it doesn...it would have helped me :)


Permalink

Answer 1447 (0.0% helpful)

I got this problem in the following scenario:
I had retrieved a persistent object A, which contained references to persist object B, which in turn contained references to persistent object C. Could reference A and B no problem, but because of the lazy initialising, trying to read from B to C caused this error. The solution was to re-obtain B locally, forcing C to be pulled into scope.
Permalink

Answer 974 (0.0% helpful)

This was giving error
getHibernateTemplate().load(Airline.class, code);

I changed to get and it worked.
getHibernateTemplate().get(Airline.class, code);
Permalink

Answer 1027 (0.0% helpful)

make it a transactional unit of work. why need to make Lazy = false, you just remove one of the benefits of using Hibernate... RMA
Permalink

Answer 1058 (0.0% helpful)

This can also be caused by clearing your (JPA) persistence context and then trying to access the lazily-loaded attributes.  (You\'d need to defer the clearing of the context until after you\'ve finished with the persisted objects.)
Permalink

Answer 1114 (0.0% helpful)

I put in all my hbm files lazy to false. But, now, i am wondering, \"why provide a lazy attribute that works only with false\" !!!
Permalink

Answer 1121 (0.0% helpful)

Answer 12 works perfect!, putting the attribute to lazy also works, but the system performance will get slow
Permalink

Answer 1232 (0.0% helpful)

use Spring\'s OpenSessionInViewFilter:

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

<filter-mapping>
	<filter-name>hibernateFilter</filter-name>
	<url-pattern>/spring/*</url-pattern>
</filter-mapping>

You might also need to reattach your object to Hibernate.

Permalink

Answer 1306 (0.0% helpful)

answer 22: +1 (in my case, using spring+stripes)

reason:
allow for lazy loading in web views despite the
 original transactions already being completed. 
Permalink

Answer 1386 (0.0% helpful)

lazy load - i did nto try, obviously not a good solution.

web.xml - didn\'t work for me.

My scenario is having master-detail relationship of a table referring to itself.  I have one special record that has a foreign key that don\'t exist.  I had to make a work around here:  On rename of a record, works as usual, but on adding a new record that refers to my special record as its master, i need to force update() on the master first, before calling saveOrUpdate().

I use, spring2, hibernate3, java6




Permalink

Add an answer/solution

If you know the answer, please add your own solution below.
If you don't know, but find out later, please come back and share your answer - there will be other people struggling with this too.


Please enter 61948 here

If you want to be notified via email when this is solved, enter your email address here: