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.
|