java - Deserialize an object after the underlying class structure got changed -
we serializing data objects , storing them in database, there hundreds of such objects in database since longtime. needed change structure of 1 of classes. deserializing object throwing errors.
we have not written custom serialization , deserialization earlier. use of defaultserialversionuid not helping here default 1l while serializing them.
is there better way deserialize them after underlying class structure got changed?
we serializing data objects , storing them in database ...
that was/is big mistake, imo. avoided having build , maintain tables corresponding data objects ... java object serialization not designed kind of thing, , kind of fragility 1 of downsides.
@ejp , @sidoh's answers provide ideas digging out of current hole provided can hold of old versions of classes. there couple of approaches:
roll code / schema state before changes broke compatibility ... , redo work in way avoids mistake.
don't rollback, write ad-hoc converter loads readable objects readable form , updates stored versions.
the former may impractical; e.g. if else has changed, or if code / data in production.
the latter involves retrieving old , new versions of classes version control , building ad-hoc converter loads old classes, translates instances of new classes , saves. difficulty of building java application can simultaneously use 2 different versions of same classes in same jvm:
you can instantiate 2 classloaders different versions on classpaths. problem jvm treat 2 sets of classes different types, , make static binding against both versions impossible. deal via reflection ... messy, if respective object apis extensive.
you two-stage process. stage 1 load using old classes , serialize classes using (say) json write file (say). stage 2 read json, use create objects using new classes, , serialize them using object serialization.
a third alternative write ad-hoc converter tweaks serialized objects. basically, need head around differences between old , new serialized forms , rewrites serialized objects reading / writing them using low-level api. variation of implement custom readobject , method in new version of class understands both old , new formats. however, tricky given old objects don't have custom version fields. should noted kind of stuff beyond scope of serialization spec ...
but imo best alternative use opportunity stop using java serialization. read objects using old classes, , write them database regular sql tables, json or xml blobs, or using orm mapping.
Comments
Post a Comment