aspose file tools*
The moose likes Java in General and the fly likes How to deserialize persistent object if class has changed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to deserialize persistent object if class has changed" Watch "How to deserialize persistent object if class has changed" New topic
Author

How to deserialize persistent object if class has changed

Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Hi

I have a scenario where I am upgrading the version of a component.
An instance of a class from this component is used as a persistent object.

I saved some objects with the older version and now want to retrieve it. I know the changes in the class, can get my data from the persistent object ?

Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

The serialVersionUID must remain the same or deserialization will fail. After that it should work; dropped fields will be ignored, and added fields will be set to defaults (null / 0 / '\0' / false).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Thanks Rob,

My serialVersionUID is the same, but datatypes of some fields have changed.
Some new fields have also been added, but as yosay they should be handled.
Also, the toString() method has changed
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

The only problem there would be the changed data types. I don't think serialization can handle that, but you would have to try first to make sure. Maybe it will work if primitive types are widened, or if reference types are assignable, but I doubt it to be honest.
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

A String is changed to int
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Then I am afraid you have a problem. I wouldn't know right away how to solve this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
Rob Prime wrote:I wouldn't know right away how to solve this.
Do you think it is possible to solve this at all? If you change a String to an int in a class, you have to all intents and purposes created a different class. It is hardly going to serialise and deserialise at all.
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

I am trying to keep the String as a String with the same name and create a new int for the other variable..
Will test it and update what happens
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Aniruddh Joshi wrote:I am trying to keep the String as a String with the same name and create a new int for the other variable..



Worked

thanks guys!
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 242

Instead of Serializable, use Eternalizable. Then write and read a version number. In the readExternal() method, check the version number and then read only those fields that are applicable to that version. Note that the serialVersionUID needs to be hard coded so that changes in the class do not cause the UID value to change.



Sent from my IBM 360 mainframe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to deserialize persistent object if class has changed