| |
 |
Friday, January 09, 2004 |
When you retrieve an object with Toplink, no matter if it's through the
ClientSession or a UnitOfWork, Toplink returns an object that it is
tracking in its cache. If you change the object in one place, ask
for it again in the other, you will get the changed object. In
some cases this is great because you can quickly and easily reflect the
latest information with incredible performance. However I find
myself being confused by this behavior for most of my applications.
My idea is that each of the classes you store with Toplink implements Cloneable. Then, in your DAO layer (you are using
a DAO layer, right?) , before you return an object to the caller you
call its clone() method. This will ensure that you have a shallow
copy of the object that is not associated with Toplink's cache. Then,
you can count on 2 retrievals of the same object only reflecting any
updates that have actually been committed to the cache/database.
If you have a complex object graph that you are persisting with Toplink
and you want the same functionality, you can implement a method to do a
deep clone. This would make sure that the entire graph reflects
the data at that point in time rather than any changes that are
happening in other places.
3:47:30 PM
|
|
© Copyright 2004 Tom Pierce.
|
|