cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tomi NA" <>
Subject Re: serialization issue
Date Sat, 20 May 2006 09:24:39 GMT
On 5/18/06, Andrus Adamchik <> wrote:

> A simple trick would be to use thread-bound DC and create
> "readResolve" method in a custom DO superclass that reattaches itself
> to the thread context (or rather returns a substitute object local to
> the current DC):
> private Object readResolve() throws ObjectStreamException {
>     DataContext dc = DataContext.getThreadDataContext();
>     return dc == null ? this : dc.localObject(getObjectId(), this);
> }

This brought me closer to the solution, but I'm not there yet.
Here's what I did.
My dc is in a public static field instead of the current thread - I'm
not sure I can depend on always being in the same thread and I'd like
to use the same dc during the runtime of the app.
I've extended CayenneDataObject along the lines you suggested:

public class SerializableCayenneDataObject extends CayenneDataObject {

    public SerializableCayenneDataObject() {

    private Object readResolve() throws ObjectStreamException {
     DataContext dc = Misc.context;     // not from the current thread
     return dc == null ? this : dc.localObject(getObjectId(), this);

I've regenerated the generated superclasses so that they extend the
SerializableCayenneDataObject instead of CayenneDataObject.
Alas, the breakpoint I placed inside the readResolve method never gets
called and I still get an object with null values for all fields
instead of the object I stored.

I tested then if the readResolve code would work anyway and included
it into the readExternal() deserialization method. This is what it
looks like:

    public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
        try {
            MyEntityDataObject medo = (MyEntityDataObject)in.readObject();
            medo = (MyEntityDataObject)
Misc.dataContext.localObject(medo.getObjectId(), medo);
        } catch (Exception ex) {

    public void writeExternal(ObjectOutput out) throws IOException {

Anyway, this works. I get a medo object with the information I stored
using writeExternal. What I really need now is a way to make the
DataContext.localObject call transparent i.e. have it called
internally in my dataObject by extending my custom DataObject. Can
anyone tell me what I did wrong?

View raw message