Pregunta

We have a very old class which doesn't have serialVersionUID.

4 years back someone added serialVersionUID to this class with a random value. He should have used serialver tool to find the value and used it while adding serialVersionUID, but that was not the case.

There are customers using both the versions of this class.

Now if I want to deserialize the old customer's objects is there any way out. I can't change the current serialVersionUID since the new customers will be affected.

¿Fue útil?

Solución

You will have to write your own readObject() method that can handle the different UIDs.

"Versioning of Serializable Objects" explains what the UID means and what the rules are.

This answer goes into more details.

I tried to find a good example how to write readObject() for some object that has several UIDs but couldn't find any. There isn't even an example in "Effective Java" by Joshua Bloch. It just says "If you change the UID, you will get an error" but not how to handle this error gracefully.

This question at least comes close: How to deserialize an object persisted in a db now when the object has different serialVersionUID

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top