Reputation: 703
I am trying to update some hibernate code that was written by someone else in the past and running into the deserialization issue. The way original code was written, it did not have serialVersionUID explicitly declared and just implemented Serializable interface -
public class SamplePOJO implements Serializable {
Now, I am trying to add a new column to the table and map it to this object. I:
However, when I run it after compilation I get the following error -
"Error while deserializing from byte[]., caused by x.y.SamplePOJO; local class incompatible: stream classdesc serialVersionUID = 7997933458932550222, local class serialVersionUID = <other number internally auto generated as source didn't explicitly mention serialVersionUID>"
If I update the code to include the serialVersionUID that matches the one thrown in the above error, it executes without any issues.
Based on what info I have found the most common cause seem to be different hibernate jars in client and server. However, that is not the case here as it uses the same hibernate jar. Could someone help if there is a way to fix this problem without having to specifically mention the serialVersionUID that's thrown during the exception? Also, if I have to stick with this approach and if my code moves to another environment (qa/prod), would it expect different serialVersionUID depending on how it is serialized in other environments?
I will appreciate any/all help!
Upvotes: 3
Views: 1202
Reputation: 1183
To answer your second question first, the generation of serialVersionUID at runtime by the JVM is specified by the Java Object Serialization Specification. Whilst the generation of the serialVersionUID at runtime is predicatable, it can vary between java compilers as it is dependant on composition of the class file which is compiler implementation specific. So your compiled code should not generate different serialVersionUIDs at runtime in different environments. You can use the serialver command from the JDK to calculate the runtime generated serialVersionUID for a class.
You talk about having the hibernate jar on the client and the server, this sounds like you are serializing your entities between two different JVM's, are you sure the client has the updated entity class? The error indicates that you are attempting to deserialize a byte[] generated from the serialization of a different version of the class file. You either need to ensure you are deserializing into the same version of the class or add the serialVersionUID to the class.
Upvotes: 1