Deserializing Object which had different SerialVersionUID than current One?
Hi guys,
Please help me on this topic. i ll explain what i have done as a newbie to Serialization.
I have a Class A with serialVersionUID = 5L.
I have serialized this class and stored in DB.
I can deserialize that class and use it using same class.
Now i want to change some fields (add modify and delete ) in class which accordingly ask me to change my serialVersionUID.
If i deserialize using new class i cannot deserialize it.
Now my questions are:-
How should i check my SerialVersionUID of object stored in DB?
How can i deserialize old Object?
Please ppl help.................
[649 byte] By [
yogeshg15a] at [2007-11-26 13:08:55]

# 1
> Now i want to change some fields (add modify and
> delete ) in class which accordingly ask me to change
> my serialVersionUID.
Who asked you to do that? It's the wrong thing to do. You should leave the serialVersionUID strictly alone unless you are doing one of the things defined as serialization-incompatible in the Serialization specification, Versioning chapter. And you shouldn't be doing that to a a class that you already have serialized instances of.
I suggest you read that chapter thoroughly.
ejpa at 2007-7-7 17:20:57 >

# 2
Hi ejp thanks for replying...........
Well i read the Serialization specification doc from Sun.And its really gud.Thanks for the same.
Now why would i want to add new fields ......The product i m working now is still evolving and we are focussing on performance through Serialization. Though we dont know what new features in our product might ask us to add some more fields..So we want to handle this by knowing SerialVersionUID and handling the changes in previous and current classes while readObject() written by us......
So i would like to know SerialVersionUID from the stream.......
There is a method in ObjectInputStream readObjectClassDescriptor() but that one is protected from which i can access serialVersionUID.
The problem i m facing is that readObjectClassDescriptor() method requires to know that next in the stream is ObjectClassDescriptor...
Help my language might not be informative but i have tried to sum up ......
Waiting for ur reply.....!!
# 3
If you've really read it you would know that adding fields is serialization-compatible.
Otherwise I can only repeat what I said before. You're barking up the wrong tree. Forget about changing the serialVersionUID, or trying to read it from the stream. Forget about trying to fool the serialization system. Use it the way it was designed, and concentrate on what you have to do to make your classes serialization-compatible according to the specification.
If your classes are changing so dynamically that you can't manage this, forget about serializing instances of the old version and use something more reliable over the long term, e.g. java.beans.XMLEncoder. This is designed for long-term persistence of evolving classes.
ejpa at 2007-7-7 17:20:57 >

# 4
Well...................I found teh answer
for knowing SerialVersionUID of previously serialized object of Class.
Use this method.....
public static long getSerialVersionUID(DataInputStream RInputStream) throws IOException{
byte byt = RInputStream.readByte();
while(byt!=ObjectStreamConstants.TC_CLASSDESC){
byt = RInputStream.readByte();
}
String name = RInputStream.readUTF();
long uid = RInputStream.readLong ();
byte flags = RInputStream.readByte ();
short field_count = RInputStream.readShort();
return uid;
}