Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

(De)Serialization question

 
Roel De Nijs
Sheriff
Posts: 10594
143
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have an object (that implements Serializable-interface) written to a file some days ago. no problems occured.

today i wanted to read the object back from the file, but the object that's in the file contains some other members of other classes (all implementing Serializable interface) and some of this classes are changed (new members, methods, etc.)

when i try to readObject from file, i got this error:

INFO: Initializing, config='org.apache.struts.taglib.html.LocalStrings', returnNull=true
java.io.InvalidClassException: be.aquafin.webgis.kaart.ComplexKaart; local class incompatible: stream classdesc serialVersionUID = -1104219041991736069, local class serialVersionUID = 150420720706126891

so if i save object to file, change some classes, try to get file back, i will always get error. or could i prevent this from happening ?

thanks for your help
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could add a serialVersionUID to all your Serializable classes. If you haven't defined this then the JVM will try to derive a value for it based on the methods and fields of the Serializable class. So if you've changed/added methods the derived serialVersionUID will be different, but if you define it yourself it will remain unchanged.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, once you explicitly define serialVersionUID, you also have to
take on versioning: Is the new version of your class compatible with
the old, as far as serialization is concerned?
[ October 19, 2005: Message edited by: Jeff Albrechtsen ]
 
Roel De Nijs
Sheriff
Posts: 10594
143
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and the serialVersionUID has it to be different for each class, or could i add it to the top of a hierarchical tree and make it protected so all subclasses have the same serial version
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's not inherited. Every class should define its own, or
the serialization process will compute a value on the fly.
 
Roel De Nijs
Sheriff
Posts: 10594
143
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and is the compatibility between old version and new version depending on the variables, the methods or both
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you define your own serialVersionUID, then its all up to you.
If you are letting the JVM calculate it, well, try the following
experiment...

Run this code to write the file, then again to read it. Then
uncomment method f and run it again.
 
Roel De Nijs
Sheriff
Posts: 10594
143
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the point
thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic