hello all,
I'm working on an application with Hibernate. When a record is
modified, instead of updating that DB row, a new row is inserted and
the original row is marked obsoleted, so that all user actions can be
traced back when there's need.
As an example, now I update the field quantity from 1 to 20. Here is
how I do it with hibernate:
...
newInfo = sess.load( key );
newInfo.setQty( 20 );
// update the old rec
oldInfo = sess.load( key );
oldInfo.setObsoYn( "Y" );
sess.update( oldInfo );
// add a new rec
sess.save( info );
trans.commit();
...
Sadly, newInfo and oldInfo seems to be pointing to the same instance,
and after all the actions I end up getting both records to have
qty==1.
Is there any way I can avoid them to interfere with each other?
thx
george
hilz - 28 Sep 2004 16:47 GMT
I am no hibernate expert, but i think what you need to do is instead of
loading both newInfo and oldInfo from the database, you should just load
the oldInfo, and then copy its values to newInfo which should be an instance
of Info that you create:
newInfo = new Info();
//copy the values from oldInfo to newInfo.
//or maybe clone oldInfo, i am not sure. try it!
session.save(newInfo);
the reason you were experiencing that behavior is because you were loading
newInfo from the database, so ofcourse it is going to be pointing to the
same row. but if you create the instance by yourself, it is will be
considered a new object , and thus a new row.
HTH.
hilz
> hello all,
>
[quoted text clipped - 28 lines]
> thx
> george