Lazy Initialization in Hibernate

J

J

I am using Hibernate for the first time. There are one-many
relationships where we want to use Lazy Initialization.
I am setting lazy = "true", but the problem is when i try to get the
child object later, i get the error : LazyInitializationError - no
session.
What is the way to solve this problem.
Also i want to know if i can use Lazy Initializations when there are
JMS calls.
Lazy Initialization means - that particular information is retrieved
only at the time when we access the object. So at that time will it
make a JMS call?
 
H

H.MuthuKumaraRajan

We are using a session to load objects, but the problem is the session
that loads the parent object is not available for loading the child
objects, is there any mechanism that we can use so that the same
session is retained for both the objects or is there any way through
which we can use a different session for loading the child objects
alone.

Thanks in advance.
 
C

Chris Smith

H.MuthuKumaraRajan said:
We are using a session to load objects, but the problem is the session
that loads the parent object is not available for loading the child
objects, is there any mechanism that we can use so that the same
session is retained for both the objects

Yes; don't call Session.close() until after you're really done with the
Session.
or is there any way through
which we can use a different session for loading the child objects
alone.

You can reattach an object to a new session using Session.lock, and
passing LockMode.NONE as the parameter.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
H

H.MuthuKumaraRajan

Thank you for the help, would be great if there are some sample codes
or reference links which could explains us the usage of 'Session.lock'
through which we can use lazy initialization in hibernate.
 
C

Chris Smith

H.MuthuKumaraRajan said:
Thank you for the help, would be great if there are some sample codes
or reference links which could explains us the usage of 'Session.lock'
through which we can use lazy initialization in hibernate.

It's not really difficult at all. If you've closed the session that you
used to create the object in the first place, then you need to associate
a new Session with the object in order to perform operations like that.
This means getting the new Session, and then writing:

session.lock(theObject, LockMode.NONE);

Because the mode is LockMode.NONE, this doesn't actually obtain a lock;
but rather just associates the object with the session and leaves it
unlocked.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
H

H.MuthuKumaraRajan

but when we used session.lock(theObject, LockMode.NONE), we get the
following exception net.sf.hibernate.TransientObjectException:
attempted to lock a transient instance
What could be the problem?, thanks in advance.
 
K

Keith James

H> but when we used session.lock(theObject, LockMode.NONE), we get
H> the following exception
H> net.sf.hibernate.TransientObjectException: attempted to lock a
H> transient instance What could be the problem?

Hibernate forum ("lock AND transient"):

http://forum.hibernate.org/viewtopic.php?t=927206&highlight=transient+lock

"session.lock(object, LockMode.NONE) is working since 2.1. only"

It's hard to tell because you don't supply any code or stack trace.

You are using a recent version of Hibernate?
 
H

H.MuthuKumaraRajan

No, the hibernate version we are using is 2.0.2, is there any way by
which we can achieve session locking in the version that we have, or
else can you suggest some ways on how to achieve lazy initialization
with hibernate v 2.0.2.

thank you.
 
K

Keith James

H> No, the hibernate version we are using is 2.0.2, is there any
H> way by which we can achieve session locking in the version that
H> we have, or else can you suggest some ways on how to achieve
H> lazy initialization with hibernate v 2.0.2.

Wow, that's an old version! (Which has a bug in Map.putAll() and a
couple of caching bugs.)

See the changelog at

http://sourceforge.net/project/shownotes.php?release_id=242817

Under the section "Changes in version 2.1 beta 2 (27.8.2003)"

* transient object may now be associated with session using Session.lock()

This is where it was first implemented.

See previous posts in this thread for your available solutions. The
simplest option is not to close the Session until you are finished
with it. It's difficult to make reasonable suggestions beyond this
without a lot more information.
 
H

H.MuthuKumaraRajan

Thank you every one for the responses with very little information on
the problem, currently we could not leave the session open until we
are done with loading of related objects, because we are not sure when
the actual request for loading those objects will be initiated, that's
the reason we chose to go with the Session.lock. We'll try to use the
newer version, thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top