Serialization - more, partial solution, kind of

From: Craig Miskell (cmiskel..lbatross.co.nz)
Date: Thu Sep 12 2002 - 21:14:46 EDT

  • Next message: Craig Miskell: "[SOLVED] Serialization"

    Ok, I'm silly.

    Read documentation, then actually tried something instead of just asking
    the list.

    I added "implements Serializable" to ObjectID, rebuilt cayenne. Then in
    my webapp, I made the DataContext transient. However, then ToManyList
    became a problem (also not serializable). So, I made the data object (a
    User) transient,and in the setCurrentUser method, stored the user objectID
    in a non-transient ivar, and in the get, if currentUser was null but the
    objectID wasn't, used registeredExistingObject to pull in the data object
    fresh.
    public class Visit implements Serializable {
            private ObjectId userID;
            private transient User currentUser;
            private transient DataContext dataContext;
            /**
             * Constructor for Visit.
             */
            public Visit() {
                    super();

            }

            /**
             * Returns the currentUser.
             *..eturn User
             */
            public User getCurrentUser() {
                    if(currentUser==null) {
                            if(userID!=null) {
                                    currentUser=(User)
    this.getDataContext().registeredExistingObject(userID);
                            }
                    }
                    return currentUser;
            }

            /**
             * Sets the currentUser.
             *..aram currentUser The currentUser to set
             */
            public void setCurrentUser(User value) {
                    currentUser = value;
                    if(currentUser!=null) {
                            userID=currentUser.getObjectId();
                    } else {
                            userID=null;
                    }
            }

            /**
             * Returns the dataContext.
             *..eturn DataContext
             */
            public DataContext getDataContext() {
                    if(this.dataContext==null) {

    this.dataContext=Configuration.getSharedConfig().getDomain().createDataContext();
                    }
                    return this.dataContext;
            }

    }

    This works, but it's a lot of work for each data object that I need to
    store on a session basis. It also means that any changes to the object
    will be lost if it hasn't been "saved".

    I also am thinking that for each object I make "Serializable", I'm going
    to have to make 10 others Serializable as well.... any thoughts?

    Craig Miskell
    Programmer, Black Albatross, Otago University, New Zealand
    -----BEGIN GEEK CODE BLOCK-----
    Version: 3.1
    GCS d- s+:- a-->? C++++(++)$ ULXH+++$>++++ P+>++++ L++$>++++$ E--- W+++$
    N+ K? w--- !O M-- V? PS--- PE Y t++ 5 X+++ R-- tv+ b+>+++ DI++++ D+ G+ e++
    h--- r+++ y+++
    ------END GEEK CODE BLOCK------



    This archive was generated by hypermail 2.0.0 : Thu Sep 12 2002 - 21:14:54 EDT