- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All TopicsHi,
I'm using Spring JPA (Hibernate implementation) for my application. The web part works porperly.
But I've also have a standalone batch. It fails when I try to load data from db throwing the following exception. "org.hibernate.LazyInitial
How to resolve this?
Thanks,
Madhan
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: tvedtemPosted on 2008-07-23 at 03:59:15ID: 22067787
LazyInitializationExceptio n happens whenever you have a reference to a lazily loaded object
(), or somesuch - you can probably figure out ways of doing this neatly in your system without needing to create loads of new methods).
(i.e. one only partially populated; default-lazy="true" or lazy="true" in your hbm config), and then using this reference, you attempt to access a non-populated field.
So if I have, say, a Zoo class, with some Penguins:
<one-to-many class="com.zoo.Penguin" />
and have defined default-lazy="true" at the top of Zoo.hbm.xml, then any attempt to do the following *outside of the scope of the hibernate session that originally loaded zoo*...
Set<Penguin> penguins = zoo.getPenguins();
... will result in a LazyLoadingExceptiion. Hibernate cannot figure out how to get hold of the data you're asking for.
So how to fix ?
Quick and dirty way is simply to set lazy="false" for Penguins. Then a call to getZoo() will return all the Penguns for you the first time. Only do this if you are sure that it will not cause performance issues on the rest of the system (in this fairly silly case, where we'd expect a Zoo to comprise a lot of animals, so this would be a bad idea).
Another way is to figure out whether there's a way of keeping the hibernate session open (e.g. check out spring's opensessioninview - but please research the downsides of this approach).
Best way is to ensure that for whatever you're trying to do, the objects that you need are populated (by writing a specific call which gets what you need - getZooIncludingAllPenguins
If your object graph for a given object is deep but not wide, then general getFullyLoadedX methods for each class X might be OK - then you just have to write a method that will full load an object (ignoring your lazy serttings in your hibernate config).
Again, Zoo would be a poor candidate for this approach, but something like Person - with associated object Address would probably be fine.
Whatever you decide, remember that your design will directly affect the SQL that hibernate produces - so you must make sure that the SQL is sensible.