Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2008

Tip: Looking for answers? Try searching our database.

Deserialization of Java objekts -tricky!

Thread view: 
Pixelschubser - 28 Feb 2008 11:49 GMT
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.
Nathandelane - 28 Feb 2008 12:59 GMT
> Hi there,
>
[quoted text clipped - 50 lines]
>
> 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
GArlington - 28 Feb 2008 13:04 GMT
> Hi there,
>
[quoted text clipped - 50 lines]
>
> 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...
Roedy Green - 29 Feb 2008 03:01 GMT
>Later I made a little change within the class:

see http://mindprod.com/jgloss/serialization.html#SERIALVERSIONUID
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.