2

I found that many of the serialVersionUID of the entity classes in my project use the same value.

Now,there is no problem with my project, CURD is OK, but I think it is abnormal to use same value.

Is there a problem?enter image description here

  • 1
    That shouldn't be a problem. There is lengthy discussion about that field at below link, but in short there is nothing to worry about with having same value for multiple classes like that https://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it – Don Ha Jan 05 '21 at 02:32

1 Answers1

11

There won't be any problem, as the serial number works per class.

The compiler uses the serialVersionUID to compare the versions of the class to guarantee that the same version of that class was used for serializing it when you try to deserialize it.

The serialVersionUID facilitates the versioning of serialized data. If you update some of those classes, you'll be able to change their serial version in order to avoid any deserialization with an older version of the class: As the UIDs won't match, the deserialization won't be successful. This guarantees data consistency through different versions of the serialized elements.


No problem

In resume: There's no issue if more than a class hold the same SerialVersionUID (f.e: 1L), because as said, it works per class (there's no "duplicate version UID classes" mechanism involved here). It's just as correct as two elements that were updated independently and now both hold, as a coincidence, 696969L as their serial id: it's just each one's version number.

Declaring a permanent value, for example, 1L, is like telling the compiler: trust me, this class didn't change, just deserialize it.

1L is also the default serial version UID given to classes that implement the Serializable interface, meaning "this is the first version"; It will also become the last version for those classes that won't change.


Just to finish, the serialVersionUID of a class that does not implement Serializable is 0L.

Java Object Serialization Specification

If the class has defined serialVersionUID it is retrieved from the class.

If the serialVersionUID is not defined by the class, it is computed from the definition of the class in the virtual machine.

If the specified class is not serializable or externalizable, null is returned. The lookupAny method behaves like the lookup method, except that it returns the descriptor for any class, regardless of whether it implements Serializable. The serialVersionUID of a class that does not implement Serializable is 0L.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
aran
  • 10,978
  • 5
  • 39
  • 69