-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Updating a detached instance
PostPosted: Tue Feb 20, 2007 12:43 pm 
Newbie

Joined: Tue Jan 30, 2007 8:44 am
Posts: 6
Hi I have two methods one to get a persisted object and one to update a certain value of an object, like below

private ClassA getByPrimaryKey( int id )
{
LOGGER.fine( "Looking for ClassA entry with primary key " + id );

ClassA retrieved = null;
Integer primaryKey = new Integer( id );
Session sess = sessionFactory.openSession();
try
{
sess.beginTransaction();
retrieved = ( ClassA )sess.load( ClassA .class, primaryKey );
sess.getTransaction().commit();
}
catch ( Exception e )
{
LOGGER.severe( "Trouble in getByPrimaryKey " + e.getMessage() );
sess.getTransaction().rollback();
}
finally
{
sess.close();
}
return retrieved;
}

/**
* Set the status in the ClassA to an appropriate value
* @param a the ClassA entry to be updated
* @param status The value to set the ClassA's ClassB
*/
private void setTaskStatus( ClassA a, byte status )
{
ClassB b = new ClassB ( status );
Session sess = sessionFactory.openSession();
try
{
sess.beginTransaction();
a.setClassB ( b);
sess.saveOrUpdate( a);
sess.getTransaction().commit();
}
catch ( Exception e )
{
sess.getTransaction().rollback();
LOGGER.severe( "Problem setting ClassB " + e.getMessage() );
}
finally
{
sess.close();
}
}

If I first retrieve a persisted instance then try and update it I get the following error 'Problem setting ClassB could not initialize proxy - the owning Session was closed'
Ive read the documentation regarding persisting detached instances and I seem to being doing as told, I have also read something about lazy initialization, could this be my problem? If so how would I go about fixing it.

Thanks,

M.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 4:13 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
Your transactions are too small in scope. You should not be starting and ending a transaction for each method. This is causing your problem. A Sesson only lasts as long as a transaction. When you load a persistent object using a session, then end the transaction, that session is gone, even though the persistent object is still around. When you begin a new transaction with a new session, it isn't aware of the persistent object you loaded into the other session. That is why you are getting the error. The persistent object is detached from the point of view of the new session, so an "update" is not a valid operation for that persistent object.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 5:59 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
Hi Mike,

as has been said running all the code in a single transaction would be the easiest way to solve your Problem.
The Problem doesn't occur during the hibernate update-Instruction, it already occurs in the a.setClassB(), since a's old ClassB is a hibernate-proxxy-object (because lazy-loading is enabled) and a is detached in your new session.

If this can't be done you could try reversing the oder of two of your statements:

sess.beginTransaction();
[red]
sess.saveOrUpdate( a);
a.setClassB ( b);
[/red]
sess.getTransaction().commit();

Now you first reattach a to your current session (via saveOrUpdate()) and then access a's ClassB.

Hope this works....

Greets

piet


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 6:58 am 
Newbie

Joined: Tue Jan 30, 2007 8:44 am
Posts: 6
Thanks for your help guys.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.