Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to deserialize persistent object if class has changed

 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String is changed to int
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I am afraid you have a problem. I wouldn't know right away how to solve this.
 
Campbell Ritchie
Sheriff
Posts: 48642
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1378
16
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic