This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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


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: 20078
    
  30

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 - OCEJPAD 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: 20078
    
  30

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: 20078
    
  30

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: 43975
    
  33
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: 719
    
    2

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.



When I was your age I was toggling in the bootstrap code on the front panel
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: How to deserialize persistent object if class has changed
 
It's not a secret anymore!