Deserialization of Java objekts -tricky!

Discussion in 'Java' started by Pixelschubser, Feb 28, 2008.

  1. Hi there,

    I got a really big problem and hope, that anyone of you can help me
    out.

    Here we go:

    I wrote an Java-application which contains a class called "MyClass".
    This class has various members, like:

    ....
    int id;
    Strinh hersteller;
    String farbe;
    boolean verkauft;
    ....

    The class also contains getters and setters etc.

    Now, if a create a new instance of this class, it will be written as a
    serialized object into my database. It works fine, even deserialzation
    works.
    For better understanding lets call this version of the class "MyClass"
    version 1.

    Later I made a little change within the class: I only added one more
    method (it does't matter what this method does). There nothing else
    which is changed in the class.
    As is did it before, I create instances of this class and put it as a
    serialized object into my database - works fine. Even the
    deserialization sems to work.
    Lets call this version of the class "MyClass" version 2.

    Who knows some things about serialisation of objects, can imagine what
    happens now.

    Now there are two types of serialiazied objects in my database,
    depending on the same class. Because they have different serialUIDs, i
    can't now deserialise Objects from version 1 !

    My Problem: I need both objects !

    Code:
    InputStream is = rs.getBlob(1).getBinaryStream();
    ObjectInputStream oip = new ObjectInputStream(is);
    Object testObject = null;
    testObject = oip.readObject(); // Hier fliegt er dann weg !!

    Now it is too late, to put the serialUID just as a Long value into the
    class. But how can i solve my problem in any way?

    Has anyone an idea?

    Thx a lot.
     
    Pixelschubser, Feb 28, 2008
    #1
    1. Advertisements

  2. Pixelschubser

    Nathandelane Guest

    On Feb 28, 4:49 am, Pixelschubser <> wrote:
    > Hi there,
    >
    > I got a really big problem and hope, that anyone of you can help me
    > out.
    >
    > Here we go:
    >
    > I wrote an Java-application which contains a class called "MyClass".
    > This class has various members, like:
    >
    > ...
    > int id;
    > Strinh hersteller;
    > String farbe;
    > boolean verkauft;
    > ...
    >
    > The class also contains getters and setters etc.
    >
    > Now, if a create a new instance of this class, it will be written as a
    > serialized object into my database. It works fine, even deserialzation
    > works.
    > For better understanding lets call this version of the class "MyClass"
    > version 1.
    >
    > Later I made a little change within the class: I only added one more
    > method (it does't matter what this method does). There nothing else
    > which is changed in the class.
    > As is did it before, I create instances of this class and put it as a
    > serialized object into my database - works fine. Even the
    > deserialization sems to work.
    > Lets call this version of the class "MyClass" version 2.
    >
    > Who knows some things about serialisation of objects, can imagine what
    > happens now.
    >
    > Now there are two types of serialiazied objects in my database,
    > depending on the same class. Because they have different serialUIDs, i
    > can't now deserialise Objects from version 1 !
    >
    > My Problem: I need both objects !
    >
    > Code:
    > InputStream is = rs.getBlob(1).getBinaryStream();
    > ObjectInputStream oip = new ObjectInputStream(is);
    > Object testObject = null;
    > testObject = oip.readObject(); // Hier fliegt er dann weg !!
    >
    > Now it is too late, to put the serialUID just as a Long value into the
    > class. But how can i solve my problem in any way?
    >
    > Has anyone an idea?
    >
    > Thx a lot.


    Well, let's see you say that you created two versions of your class -
    have you retained both versions of the class? If you have, then you
    probably need to create some method of conversion so that you will be
    able to read your old serialized objects as new ones. I suggest
    recreating your old class, if that means removing the things you added
    by commenting them out then fine. Do that, and see if you can thereby
    retrieve the old serialized objects. If so, then create a new class
    resembling the new version of your class and create a small main that
    converts the deserialized old version of the object to a new version
    of the object and reserializes it and replaces the blob in your
    database. It seems like this should work. Let us know.

    Nathan
     
    Nathandelane, Feb 28, 2008
    #2
    1. Advertisements

  3. Pixelschubser

    GArlington Guest

    On Feb 28, 11:49 am, Pixelschubser <> wrote:
    > Hi there,
    >
    > I got a really big problem and hope, that anyone of you can help me
    > out.
    >
    > Here we go:
    >
    > I wrote an Java-application which contains a class called "MyClass".
    > This class has various members, like:
    >
    > ...
    > int id;
    > Strinh hersteller;
    > String farbe;
    > boolean verkauft;
    > ...
    >
    > The class also contains getters and setters etc.
    >
    > Now, if a create a new instance of this class, it will be written as a
    > serialized object into my database. It works fine, even deserialzation
    > works.
    > For better understanding lets call this version of the class "MyClass"
    > version 1.
    >
    > Later I made a little change within the class: I only added one more
    > method (it does't matter what this method does). There nothing else
    > which is changed in the class.
    > As is did it before, I create instances of this class and put it as a
    > serialized object into my database - works fine. Even the
    > deserialization sems to work.
    > Lets call this version of the class "MyClass" version 2.
    >
    > Who knows some things about serialisation of objects, can imagine what
    > happens now.
    >
    > Now there are two types of serialiazied objects in my database,
    > depending on the same class. Because they have different serialUIDs, i
    > can't now deserialise Objects from version 1 !
    >
    > My Problem: I need both objects !
    >
    > Code:
    > InputStream is = rs.getBlob(1).getBinaryStream();
    > ObjectInputStream oip = new ObjectInputStream(is);
    > Object testObject = null;
    > testObject = oip.readObject(); // Hier fliegt er dann weg !!
    >
    > Now it is too late, to put the serialUID just as a Long value into the
    > class. But how can i solve my problem in any way?
    >
    > Has anyone an idea?
    >
    > Thx a lot.


    Do you have a previous version of your class (in CVS...)?
    If you do - instantiate object of previous version, run your de-
    serialization, cast to new version of your class, serialize and save...
     
    GArlington, Feb 28, 2008
    #3
  4. Pixelschubser

    Roedy Green Guest

    Roedy Green, Feb 29, 2008
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    825
    Sahil Malik
    May 14, 2004
  2. =?Utf-8?B?U29sZWwgU29mdHdhcmU=?=

    Deserialization Error--End Of Stream

    =?Utf-8?B?U29sZWwgU29mdHdhcmU=?=, Jul 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    521
    Steven Cheng[MSFT]
    Jul 30, 2004
  3. =?Utf-8?B?U29sZWwgU29mdHdhcmU=?=

    Deserialization Error--End of Stream

    =?Utf-8?B?U29sZWwgU29mdHdhcmU=?=, Jul 29, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    3,689
    Marcin
    Aug 10, 2004
  4. =?Utf-8?B?TWlrZSBMb2dhbg==?=

    NameValueCollection Serialization Deserialization

    =?Utf-8?B?TWlrZSBMb2dhbg==?=, Dec 22, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,227
    Karl Seguin
    Dec 23, 2004
  5. =?Utf-8?B?QW5kcmUgRmxlZGdl?=

    design time control deserialization problem

    =?Utf-8?B?QW5kcmUgRmxlZGdl?=, Jul 28, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    408
    =?Utf-8?B?QW5kcmUgRmxlZGdl?=
    Jul 28, 2005
  6. tshad

    Deserialization error

    tshad, Oct 18, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    521
    tshad
    Oct 18, 2005
  7. Stephen Marjoribanks

    Tricky Java/XML problem

    Stephen Marjoribanks, Mar 29, 2006, in forum: Java
    Replies:
    2
    Views:
    560
    Oliver Wong
    Mar 30, 2006
  8. Replies:
    9
    Views:
    768
    CBFalconer
    Apr 25, 2006
Loading...