Deep understanding of serialversionuid in Java serialization

Source: Internet
Author: User

Author: fbysss
MSN: jameslastchina@hotmail.com
Blog: blog.csdn.net/fbysss
Disclaimer: This article is original from fbysss. for reprinting, please indicate the source
Keyword: serialversionuid serialization

I. Preface

Serialversionuid,In short, the objective is to serialize object version control and check whether each version is compatible during deserialization. If this value is modified in the new version, the new version is not compatible with the old version, and will be thrown during deserialization.InvalidclassexceptionException. If the modification is small, such as adding only one attribute, we want to be backward compatible, and the data of earlier versions can be retained, we do not need to modify it. If we delete an attribute, or, if the inheritance relationship of the class is changed, the old data is not compatible, and the version number should be updated manually, that isSerialversionuid.

For its definition, see the JDK documentation: http://download.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html

Ii. Problems

1. If serialversionuid is not explicitly set, what are the consequences?

The JDK documentation has an explanation. We recommend that you explicitly declare it, because if it is not declared, JVM will automatically generate a value for us, but this value is related to the implementation of the compiler and is not stable, in this way, deserialization may occur in different JVM environments.InvalidclassexceptionException.

... It is stronugly recommended that all serializable classes explicitly declare serialversionuid values, since the default condition computation is highly sensitive to class details that may vary depending on compiler implementations...

2. What are the differences between the two serialversionuid types?

In eclipse, two methods are provided for quick addition.Serialversionuid.

Add default serial version ID:
Adds a default serial version ID to the selected type
Use this option to addUser-DefinedID in combination with custom serialization Code if the typeDid undergoStructural change since its first release.

Add generated serial version ID:
Adds a generated serial version ID to the selected type
Use this option to addCompiler-generatedID if the typeDidnot undergoStructural change since its first release.

One is 1l, and the other is to generate a large number. What are the differences between the two types?

It seems that the class of each class is different.SerialversionuidThere is a certain association between classes. In fact, neither of them can be seen from the JDK documentation. We only need to ensure that different versions in the same class are changed according to compatibility requirements.SerialversionuidYou can.

For the first type,You need to know which situations are compatible and which are not compatible at all.Reference:Http://java.sun.com/j2se/1.4/pdf/serial-spec.pdf

You can retain the old version number when it is compatible. If it is incompatible or you want to make it incompatible, You can manually increase the version number.

1-> 2-> 3 .....

Method 2,Is the hash value generated based on the structure of the class. Adding or removing an attribute or method may change this value. I think this method applies to such scenarios:

The developer thinks that a new version number needs to be generated after each class modification and does not want to be backward compatible. The operation is to delete the original serialvesionuid declaration statement and then generate it automatically.

I personally thinkGenerally, the first method is simple. The second method ensures that the version number is changed after the class structure is changed each time, but the version number must be manually generated, instead of modifying the class. You will be prompted to update thisSerialversionuid,So although it looks cool, it is actually confusing.

 

Refer:

1. a good description of serialvesionuid:

Http://www.mkyong.com/java-best-practices/understand-the-serialversionuid/

2. serialvesionuid Discussion

Http://stackoverflow.com/questions/888335/why-generate-long-serialversionuid-instead-of-a-simple-1l

3. compiler-generated ID Generation Algorithm

Http://java.sun.com/javase/6/docs/platform/serialization/spec/class.html#4100

 

Other related questions:

Persistence of HibernateThis generally refers to the persistence of data to the database, and there is no direct relationship with serialization.

The pojo of hibernate does not require the serializable interface to be implemented either. However, as a system extension, pojo should implement the serializable interface for all the Po, because if these objects need to be cached on the disk, or in a distributed environment, serialization is required. The most common examples are ehcache and memcached. Objects in key and value must be serialized objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.