Deserialization of Java objekts -tricky!

P

Pixelschubser

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.
 
N

Nathandelane

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
 
G

GArlington

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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top