This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

LaxyInitialization Error

Jun 9th, 2008, 12:33 AM

Hi,
I am new to Hibernate and Spring. I am getting the lazyInitialization error in my application. I am using struts2 with hibernate and spring. This seems to be a common problem and I found many posts on it. But I am unable to find a common solution for this problem.

Now I am getting another error
'No bean named 'sessionFactory' is defined'.

This is because I have two 'applicationContext-dao-.xml' files with beans sessionFactoryMediware and sessionFactoryClinical defined in them. I need to use these two beans separately. So I dont have the sessionFactory bean defined. This results in the above mentioned error.
Is there any other way to solve the lazy initialization problem?

You can specify which SessionFactory to use (check the javadocs for the OpenSessionInViewFilter) however that still wouldn't solve your problem as you are working with 2 sessionfactories. Do you really need 2 sessionfactories, you only would need them if you would be working with a completly different database, if it is the same database you could work with one.

However your problem is a bit more complicated.

Comment

Hi,
Yes I do need two sessionFactories since I need to access two databases in my application (Mediware and Clinical). Is there a way to solve this problem? Any pointers would be very helpful. I am stuck with this for the past 2-3 weeks.

The easiest solution is to make sure that you pre-fetch all the stuff you need in your view. However that would require you to modify your service/data access layer. Because you then need to know what to retrieve.

Comment

Hi all,
I am also facing the same 'Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL' problem. Initially there was no such errror. Then I added 'OpenSessionInViewFilter' in web.xml to fix the lazyinitialization problem that was coming. Now this problem has cropped up.
Any idea on this issue will be greatly appreciated.