Hello
I try to use Session.lock to reassign an Object from a previous session.
"The lock() method allows the application to reassociate an unmodified object with a new session." from Manual 8.6
So I tried this:
Code:
public void run1() throws Exception {
Session _session = HibernateSession.currentSession();
Team _team = (Team) _session.load(Team.class, new Long(1));
HibernateSession.closeSession();
_session = HibernateSession.currentSession();
_session.lock(_team, LockMode.NONE);
//_team.getPlayers().iterator();
HibernateSession.closeSession();
}
Team is loaded in the first Session and is unchanged. But Session.lock throws the following Exception:
Code:
net.sf.hibernate.TransientObjectException: attempted to lock a transient instance
at net.sf.hibernate.impl.SessionImpl.lock(SessionImpl.java:1487)
at example.test.LockTest.run1(LockTest.java:29)
at example.test.LockTest.main(LockTest.java:41)
The API doc say's:
"Throw when the user passes a transient instance to a Session method that expects a persistent instance."
This is true, after closing the first Session, the object is transient.
What have I to do, to get a new session for the object Team??
Thanks
Daniel