关于在 Eclipse 中生成的 serialVersionUID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10389180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:07:11  来源:igfitidea点击:

About generated serialVersionUID in Eclipse

javaeclipseserialversionuid

提问by Tapas Bose

Is there any way to generate serialVersionUID in Eclipse serially? By serially I want to mean that if one serializable class has serialVersionUID = 1L, then when I generate serialVersionUID of another class this will be serialVersionUID = 2L.

有没有办法在Eclipse中串行生成serialVersionUID?通过串行,我想表示如果一个可序列化类的 serialVersionUID = 1L,那么当我生成另一个类的 serialVersionUID 时,这将是 serialVersionUID = 2L。

If I manually specify 1L, 2L, 3L and so on, will this can create any problem?

如果我手动指定 1L、2L、3L 等,这会产生任何问题吗?

Eclipse gave an option to choose "Add generated serial version ID", is this option safe to choose?

Eclipse 提供了一个选项来选择“添加生成的串行版本 ID”,选择这个选项安全吗?

回答by Hyman

No, it won't create any problem in any of your two circumstances:

不,它不会在您的两种情况中的任何一种情况下造成任何问题:

  • you can manually specify it incrementally
  • you can let Eclipse assign them random generated values
  • you can keep them all 1Lfor different classes
  • 您可以手动增量指定它
  • 您可以让 Eclipse 为它们分配随机生成的值
  • 您可以将它们全部保留1L用于不同的课程

The purpose of the serial UID is to forbid serialization and deserialization of same classes in different versions, especially when there is not forward compatibility (eg. new version of the class cannot be serialized/unserialized by a previous definition).

串行 UID 的目的是禁止不同版本中相同类的序列化和反序列化,尤其是在没有向前兼容性的情况下(例如,新版本的类不能被先前的定义序列化/反序列化)。

This is a circumstance that occurs really rarely, especially when working with your own project which are not part of big frameworks that rely on serialization. So you can safely do whatever you want.

这种情况很少发生,尤其是在使用您自己的项目时,这些项目不属于依赖序列化的大型框架的一部分。所以你可以安全地做任何你想做的事。

The situation in which you want to have different serials for the same class is when you are updating a class and you want to forbid the serialization of it with a previous declaration of the SAME class.

您希望为同一个类使用不同序列的情况是,当您更新一个类并且您希望禁止使用 SAME 类的先前声明对其进行序列化时。

回答by ControlAltDel

Let me answer your questions backwards:

让我倒着回答你的问题:

Add generated serial version ID... is it safe? Answer: Yes

添加生成的串行版本 ID... 安全吗?回答:是的

If you manually specify 1L, 2L, 3L is that a problem: No more of a problem than making them all 1 or any other number

如果您手动指定 1L、2L、3L 是一个问题:没有比将它们全部设为 1 或任何其他数字更重要的问题

generating serial ids serially: The answer is there is no built-in mechanism for doing this. The fact that you are asking this question leads me to believe you don't know what the serialVersionUID is for. Even if you have already read up on what this does, you should read it again.

串行生成串行 ID:答案是没有内置机制可以执行此操作。您问这个问题的事实使我相信您不知道 serialVersionUID 的用途。即使你已经阅读了它的作用,你也应该再读一遍。

回答by user207421

You shouldn't do this at all. The idea is that you keep the serialVersionUIDconstant for as long as possible during the evolution of a class, by observing the rules in the Object Versioning section of the Object Serialization Specification.

你根本不应该这样做。这个想法是serialVersionUID通过遵守对象序列化规范的对象版本控制部分中的规则,在类的演化过程中尽可能长时间地保持常量。

Your 'programming style' has nothing to do with it. It is a matter of using the thing for the purpose it was designed for. It is not designed for the purpose you are using it for. Source code control systems can do that for you.

您的“编程风格”与此无关。这是将事物用于其设计目的的问题。它不是为您使用它的目的而设计的。源代码控制系统可以为您做到这一点。

回答by ratchet freak

the java serialization supports that all classes have the same serialVersionUID

java序列化支持所有类具有相同的serialVersionUID

this is use for version control so that serialized objects from a older definition are incompatible with a newer version

这用于版本控制,以便旧定义中的序列化对象与新版本不兼容

check http://java.sun.com/developer/technicalArticles/Programming/serialization/for the specifics (scroll down to "version control")

检查http://java.sun.com/developer/technicalArticles/Programming/serialization/了解详情(向下滚动到“版本控制”)