Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / September 2004

Tip: Looking for answers? Try searching our database.

Hibernate handling of persistent objects

Thread view: 
xxgeorge - 28 Sep 2004 08:06 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.