• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

(De)Serialization question

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got the point
thanks!
 
Get out of my mind! Look! A tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic