English 中文(简体)
是什么序列号,为什么我使用?
原标题:What is a serialVersionUID and why should I use it?

缺失的<密码>射电离信号时,电离辐射发出警告。

The serializable class Foo does not declare a static final serialVersionUID field of type long

什么是<条码>航空器名称/代码>? 请举出一个例子,说明缺失的<代码> 航天飞机将造成问题。

最佳回答

The docs for java.io.Serializable 也许会像你这样说:

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender s class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named serialVersionUID that must be static, final, and of type long:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

如果一个可序列类别没有明确宣布为<代码>航空器名称/代码>,则序列化操作时间将计算出一个缺标的<代码>。 如Java(TM)目标航空规格所述,该类别基于该类别的各个方面的价值。 然而,强烈建议<>所有可序列类别明确宣布<代码> 航天飞机识别号数值,因为缺标的<代码>航天飞机识别号/代码>的计算高度敏感,可视汇编实施情况而有所不同,从而在脱硫期间产生出的<编码>InvalidClassExceptions/code>。 因此,为保证始终如一的<代码>航天器/代码>。 在不同java汇编实施过程中的价值,一个可系列的类别必须申报一个明确的<编码> 航天器识别符号值。 还强烈建议,明确无误的<代码>射电离心/代码>的声明应尽可能使用私人影射器,因为这种声明只适用于直接申报的类别——serialVersionD。 田野并不像继承成员那样有用。

问题回答

如果您仅仅因为为了执行而重新编号(如果您为<条码>的序列号/代码>而重新编号,例如,如果它储存或不储存的话,你可能不关心<条码>的代号/代码>。

如果您实际上重新使用序列化,则只有在你计划直接使用序列化储存和检索物体的情况下,才会有问题。serialVersionUID代表了你的班级版本,如果你的班级目前版本与原版本不一致,你就应当予以加固。

Most of the time, you will probably not use serialization directly. If this is the case, generate a default SerialVersionUID by clicking the quick fix option and don t worry about it.

你可以告诉Eclipse,无视这些序列 FCCC/SBI/2008/INF.1。

Window > Pviss > Java > Compiler > Errors /presss > Potential Program Problems

如果你不知道,本节中你能够提出许多其他警告(或甚至有些报告说有错误),许多人非常有用:

  • Potential Programming Problems: Possible accidental boolean assignment
  • Potential Programming Problems: Null pointer access
  • Unnecessary code: Local variable is never read
  • Unnecessary code: Redundant null check
  • Unnecessary code: Unnecessary cast or instanceof

更多。

http://www.ohchr.org。 在序列化时,其价值储存在数据中。 在进行脱射时,对同一版本进行了核对,以了解序列化数据如何与现行代码相符。

如果你想要公布数据,你通常首先使用0的<密码>射电信号,并在改变序列化数据(处理或排除非中转领域)的类别时作每次结构改革。

内在的航空化机制(in.defaultReadObject()将拒绝从旧的数据版本中进行脱射。 但是,如果您能够界定自己的readObject(-Function,读取旧数据。 然后,该海关编码可以检查<代码>航天器识别符号<>,以便了解数据中哪一种版本,并决定如何将其转换。 如果你储存能够保存几版法典的序列化数据,这一版本的技术是有用的。

但是,如此长的时间里储存序列化数据并不常见。 更常见的情况是,利用序列化机制临时撰写数据,例如,藏匿点或将数据从网络上传送到另一个具有相同版本的编码数据库相关部分的方案。

在这种情况下,你不希望保持落后的兼容性。 你们只关心确保沟通的守则基础确实具有相同的版本。 为了便利进行这种检查,你必须维持<编码>航天飞机。 就像以前一样,也不要忘记在改变你的班级时更新。

如果你确实忘记更新这个领域的话,你最终可以采用两种不同的类别,其结构不同,但采用相同的<代码>航天器/代码”。 如果发生这种情况,缺省机制(in.defaultReadObject()将无法发现任何差异,并试图使不兼容的数据脱轨。 现在,你可能会最后出现加密操作时间错误或无声故障(核田)。 这些类型的错误可能难以找到。

为了帮助这一使用案例, Java平台向您提供了一种选择,即不以人工方式订立<编码> 用户识别符号。 相反,在汇编时将产生一个分类结构的洗衣,用作补贴。 这一机制将确保你永远不会有相同的班级结构,因此,你不会得到上述这些难以操作的周期性故障。

但是,汽车产生的补贴战略背后。 也就是说,同一类别产生的胎盘在编造者之间可能有所不同(如Joon Skeet上所述)。 因此,如果你将汇编成册的编码数据与不同的汇编者进行交流,则建议用人工操作的方式保存这些编号。

如果你与前面提到的第一起使用案件中一样的数据相差,你也很可能想保持 yourself。 这样做是为了获得可读物,并对何时和如何变化拥有更大的控制权。

是什么?

<代码>SerialVersionUID是每一类别独一无二的识别符号,JVM用于比较确保同一类别在Serialization期间被装上。

如果你没有具体说明,那么对一个人进行投机就会产生更大的控制。 不同汇编者之间所产生的价值可能有所不同。 此外,有时你只是出于某种原因想禁止旧序列化物体的脱硫[backward incompatibility],在这种情况下,你只得改变序列号。

www.un.org/Depts/DGACM/index_spanish.htm javadocs 编码<>Serializable > ::

the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization.

www.un.org/Depts/DGACM/index_spanish.htm 因此,你必须申报序列 FCCC/SBI/2008/12。

条对这个专题有一些好的意见。

最初的问题要求说明为什么重要,例如,这一“编码”将有用。 我已经找到了。

页: 1 固定的汽车物体在档案系统中滞留一段时间。 同时,如果对<代码>Car类别进行修改,增加一个新的领域。 后来,当你试图读到(即海流)固定的<代码>Car物体时,请上<代码>java.io.InvalidClassException——因为所有可序列的类别都自动获得独特的识别符号。 当该类识别符号不等于固定物体的识别特征时,就会出现这一例外。 如果你真的想到,由于增加了新的领域,这一例外被推翻。 你们可以避免这种例外,通过宣布一个明确的序列号来控制海隆。 在明确宣布<代码>航天器/代码>(因此不必计算)方面,也有一些小的业绩效益。 因此,在你创建时,最好在你可以使用的类别中增加你自己的序列号识别符号,如下所示:

public class Car {
    static final long serialVersionUID = 1L; //assign a long value
}

First I need to explain what serialization is.

<>Serialization允许将物体转换成溪流,因为将物体从OR Save的网络上发送,以备日后使用。

There are some rules for serialization.

  • 只有当物体的舱面或超舱面能运行时,该物体才会具有序列性。

  • An object is serializable (itself implements the Serializable interface) even if its superclass is not. However, the first superclass in the hierarchy of the serializable class, that does not implements Serializable interface, MUST have a no-arg constructor. If this is violated, readObject() will produce a java.io.InvalidClassException in runtime

  • 所有原始类型都是可序列的。

  • 瞬间田地(有透水层)是NOT序列化的(即没有被节约或恢复)。 采用可乘飞机的班级必须标明不支持序列化的班级(例如档案流)。

  • 法定领域(有静态变体)没有序列化。

Object时,Java Rontime将序号aka、serialVersionID联系起来。

Where we need serialVersionID:

During the deserialization to verify that sender and receiver are compatible with respect to serialization. If the receiver loaded the class with a different serialVersionID then deserialization will end with InvalidClassCastException.
A serializable class can declare its own serialVersionUID explicitly by declaring a field named serialVersionUID that must be static, final, and of type long.

举一个例子来尝试。

import java.io.Serializable;

public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;
    private String empname;
    private byte empage;

    public String getEmpName() {
        return name;
    }

    public void setEmpName(String empname) {
        this.empname = empname;
    }

    public byte getEmpAge() {
        return empage;
    }

    public void setEmpAge(byte empage) {
        this.empage = empage;
    }

    public String whoIsThis() {
        return getEmpName() + " is " + getEmpAge() + "years old";
    }
}

创造宇宙 目的

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class Writer {
    public static void main(String[] args) throws IOException {
        Employee employee = new Employee();
        employee.setEmpName("Jagdish");
        employee.setEmpAge((byte) 30);

        FileOutputStream fout = new
                FileOutputStream("/users/Jagdish.vala/employee.obj");
        ObjectOutputStream oos = new ObjectOutputStream(fout);
        oos.writeObject(employee);
        oos.close();
        System.out.println("Process complete");
    }
}

物体的去向

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Reader {
    public static void main(String[] args) throws ClassNotFoundException, IOException {
        Employee employee = new Employee();
        FileInputStream fin = new FileInputStream("/users/Jagdish.vala/employee.obj");
        ObjectInputStream ois = new ObjectInputStream(fin);
        employee = (Employee) ois.readObject();
        ois.close();
        System.out.println(employee.whoIsThis());
    }
}

NOTE: Now change the seriesVersionUID of the Employee category and Save:

private static final long serialVersionUID = 4L;

And execute the Reader class. Not to execute the Writer class and you will get the exception.

Exception in thread "main" java.io.InvalidClassException: 
com.jagdish.vala.java.serialVersion.Employee; local class incompatible: 
stream classdesc serialVersionUID = 1, local class serialVersionUID = 4
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:616)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at com.krishantha.sample.java.serialVersion.Reader.main(Reader.java:14)

如果你将永远不需要将物体序列化,以通过阵列并发送/储存,那么你就不必担心。 如果是的话,那么你必须考虑您的序列VersionUID,因为该物体的旋转器将与其舱载体相符。 更多了解 Java语规格。

If you get this warning on a class you don t ever think about serializing, and that you didn t declare yourself implements Serializable, it is often because you inherited from a superclass, which implements Serializable. Often then it would be better to delegate to such a object instead of using inheritance.

因此,而不是

public class MyExample extends ArrayList<String> {

    public MyExample() {
        super();
    }
    ...
}

do

public class MyExample {
    private List<String> myList;

    public MyExample() {
         this.myList = new ArrayList<String>();
    }
    ...
}

and in the relevant methods call myList.foo() instead of this.foo() (or super.foo()). (This does not fit in all cases, but still quite often.)

I often see people extending JFrame or such, when they really only need to delegate to this. (This also helps for auto-completing in a IDE, since JFrame has hundreds of methods, which you don t need when you want to call your custom ones on your class.)

发出警告(或序列VersionUID)是不可避免的一个案例,是你从“摘要行动”中伸出援手,通常是匿名的,只是增加了“方法”。 我认为,在这种情况下有警示(因为你通常可以进行可靠的序列化,并且把这种匿名的课堂按不同版本的顺序排列),但我不敢肯定编辑如何能够承认这一点。

为了了解外地序列号动力学的重要性,人们应当了解Serialization/Deserialization是如何运作的。

When a Serializable class object is serialized Java Runtime associates a serial version no.(called as serialVersionUID) with this serialized object. At the time when you deserialize this serialized object Java Runtime matches the serialVersionUID of serialized object with the serialVersionUID of the class. If both are equal then only it proceeds with the further process of deserialization else throws InvalidClassException.

因此,我们得出结论,为使航空化/航空化进程取得成功,序列化物体的序列号识别符号必须等同于该类序列号识别符号。 如果方案管理员在方案中明确指明序列VersionUID的价值,那么无论序列化和脱硫化平台如何,这种数值将与序列化物体和类别挂钩(例如,在像管理窗口这样的平台上,可以通过使用日落或日元数据,将序列化和代谢化放在不同的平台,使用Zing JVER)。

但如果是序列 版本 当时,在任何物体的航空化过程中,Java的运行时间使用自己的算法计算时,没有由方案主管具体规定。 这一序列VersionUID的计算法各不相同。 该物体被序列化的环境也有可能使用一种JRE(ex:SUN JVER)和出现蒸气化的环境正在使用LC 15(zing)。 在这种情况下,与序列化物体有关的序列号识别符号将不同于在航空化环境中计算出来的序列号识别符号。 而脱硫则不会成功。 因此,为了避免出现这种情况/问题,方案管理员必须始终具体指明可飞行的序列号。

例如,失踪的序列号识别符号可能会造成一个问题:

I m从事 this E应用软件的工作,该软件由使用模块的网络模块组成。 网站单元将<代码>EJB模块远程连接,并作为论点通过POJO实施<代码> 可空间。

这个POJO s 班在EJB jar内包装,在WEB-INF/lib网站模块内装设。 他们实际上属于同一类别,但在我包裹EJB模块I时,没有把POJO sjar与EJB模块一起包装。

对<代码>EJB的电话由于我已宣布其<代码>航天器/代码>:

Caused by: java.io.IOException: Mismatched serialization UIDs : Source
 (Rep.
 IDRMI:com.hordine.pedra.softbudget.domain.Budget:5CF7CE11E6810A36:04A3FEBED5DA4588)
 = 04A3FEBED5DA4588 whereas Target (Rep. ID RMI:com.hordine.pedra.softbudget.domain.Budget:7AF5ED7A7CFDFF31:6227F23FA74A9A52)
 = 6227F23FA74A9A52

Don t bother, the default calculation is really good and suffice for 99,9999% of the cases. And if you run into problems, you can - as already stated - introduce UID s as the need arrise (which is highly unlikely)

一、一般情况下使用<代码>射电离心>。 我知道,它将离开 Java证书的范畴。

I would know this when I to use ObjectInputStream and ObjectOutputStream for my application or if I know a library/framework I use will use it. The serialVersionID ensures different Java VMs of varying versions or vendors will inter-operate correctly or if it is stored and retrieved outside the VM for example HttpSession the session data can remain even during a restart and upgrade of the application server.

在所有其他情况下,我使用

@SuppressWarnings("serial")

由于大多数时间的缺席,<代码>射电离心/代码>就足够了。 其中包括<代码>Exception,HttpServlet

Field data represents some information stored in the class. Class implements the Serializable interface, so eclipse automatically offered to declare the serialVersionUID field. Lets start with value 1 set there.

如果你不希望发出这种警告的话,则使用:

@SuppressWarnings("serial")

www.un.org/Depts/DGACM/index_spanish.htm 为什么使用<条码>SerialVersionUID,载于<条码>,可在Java使用<条/条>。

在<代码>航空化>期间, Java的运行时间为某类产品设定一个编号,以便以后能够将其降级。 这份编号为SerialVersionUID

SerialVersionUID is used to version serialized data. You can only de-serialize a class if it s SerialVersionUID matches with the serialized instance. When we don t declare SerialVersionUID in our class, Java runtime generates it for us but its not recommended. It s recommended to declare SerialVersionUID as private static final long variable to avoid default mechanism.

当你通过实施标记界面(java.io.Serializable宣布一个类别为Serializable时, Java使用默认的Serialization机制将这一类别持续进行下去,条件是你没有使用<代码>可再生接口对这一过程进行定制。

另见:可乘级内使用SerialVersionUID

检验标准可以核实序列VersionUID在某类执行中的位置是正确的。 消耗性具有良好的价值,即它与序列式的散射器生产相匹配。 如果你有一个有多个序列可转让的DTO项目,例如,记住删除现有的序列VersionUID和进行再造,是一种痛苦,目前唯一核实的方法(我知道)是重新为每一类产品注入活力,与旧的一类相比。 这非常痛苦。

如果你想要修订大量没有序列VersionIDD的班级,同时保持与旧班级的兼容性,那么,像英特尔利J思想这样的工具,Eclipse由于生成随机数字而没有使用,也没有在一席就使用卷宗。 我提出了以下双面文字(视窗用户的单薄膜,考虑购买一种麦克或转换成欧林区),以方便地修改序列VersionUID问题:

base_dir=$(pwd)                                                                  
src_dir=$base_dir/src/main/java                                                  
ic_api_cp=$base_dir/target/classes                                               

while read f                                                                     
do                                                                               
    clazz=${f////.}                                                             
    clazz=${clazz/%.java/}                                                       
    seruidstr=$(serialver -classpath $ic_api_cp $clazz | cut -d  :  -f 2 | sed -e  s/^s+// )
    perl -ni.bak -e "print $_; printf qq{%s
}, q{    private $seruidstr} if /public class/" $src_dir/$f
done

说的话,你把这本书封起来,就把“宇宙航道”。 随后,你在Maven或Gradle项目的基本目录中管理,如:

add_serialVersionUID.sh < myJavaToAmend.lst

The lst includes the list of java file to add the seriesVersionUID in the following form:

com/abc/ic/api/model/domain/item/BizOrderTransDO.java
com/abc/ic/api/model/domain/item/CardPassFeature.java
com/abc/ic/api/model/domain/item/CategoryFeature.java
com/abc/ic/api/model/domain/item/GoodsFeature.java
com/abc/ic/api/model/domain/item/ItemFeature.java
com/abc/ic/api/model/domain/item/ItemPicUrls.java
com/abc/ic/api/model/domain/item/ItemSkuDO.java
com/abc/ic/api/model/domain/serve/ServeCategoryFeature.java
com/abc/ic/api/model/domain/serve/ServeFeature.java
com/abc/ic/api/model/param/depot/DepotItemDTO.java
com/abc/ic/api/model/param/depot/DepotItemQueryDTO.java
com/abc/ic/api/model/param/depot/InDepotDTO.java
com/abc/ic/api/model/param/depot/OutDepotDTO.java

该书使用了正统的JDK序列Ver工具。 因此,确保您的JAVA_HOME/bin在PATH。

这个问题在乔舒亚·布洛奇的有效 Java中得到了很好的记录。 一本非常好的书和一本必须读。 我将概述以下一些原因:

序列化操作时间为每个序列可分级的序号称为Serial版本。 这一编号称为序列VersionUID。 现在有一部分Math落后于这一数目,其依据是该类界定的田地/方法。 同一类别的每一次都会产生相同的版本。 这一数字在脱硫期间用于核实序列化物体的投放人和接收人为该物体装上与序列化相容的班级。 如果接收人为与相应发件人类别不同的物体装上了一类,那么,如果该接收人将会导致电解。

如果该类别是序列的,你也可以明确宣布一个叫做“航空动力”的外地,该地必须是固定的、最终的和长期性的。 多数民主选举学会像Eclipse一样,有助于你产生这种长期扼杀。

每一物体按序编号时,均标有标本的编号。 http://stackoverflow.com/q/285793/1387612” 根据有关班级结构的信息计算。 附录一

Consider a situation - in the future you need to edit or change your class and in that case when you modify it, JVM will assign it a new UID (Suppose #444). Now when you try to deserialize the employee object, JVM will compare serialized object s (Employee object) version ID(#333) with that of the class i.e #444(Since it was changed). On comparison JVM will find both version UID are different and hence Deserialization will fail. Hence if serialVersionID for each class is defined by programmer itself. It will be same even if the class is evolved in future and hence JVM will always find that class is compatible with serialized object even though the class is changed. For more Info you can refer chapter 14 of HEAD FIRST JAVA.

简单解释:

  1. 您的序列数据吗?

    空气化基本上是向文档/溪/电离层书写类别数据。 脱空气化是指数据回到一个类别。

  2. 您是否打算生产?

    如果你只是用不进口/伪造的数据测试一些东西,那么就不担心(除非你直接测试序列)。

  3. 第一种版本吗?

    如果有的话,则制定<编码>航天动力=

  4. 这是第二、第三、等版本吗?

    现在,你需要担心<代码>航天器/代码>,并且应当深入研究。

Basically, if you don t update the version correctly when you update a class you need to write/read, you will get an error when you try to read old data.

为了说明这个领域的长篇故事,如果序列化数据能够正确脱轨,则会加以检查。 经常采用不同的方案版本进行航空化和航空化,例如服务器转换器,以显示客户和客户转换为标语。 这个领域告诉大家,双方都对这个目标有相同的想法。 这个领域有助于:

  • 您在不同地点(如1个服务器和100个客户)有多种不同的副本。 如果你改变目标,改变你的版本,并忘记更新这一客户之一,那么,你会知道他无法脱硫。

  • you have stored your data in some file and later on you try to open it with updated version of your program with modified object - you will know that this file is not compatible if you keep your version right

何时重要?

Most obvious - if you add some fields to your object, older versions will not be able to use them because they do not have these fields in their object structure.

Less obvious - When you deserialize object, fields that where not present in string will be kept as NULL. If you have removed field from your object, older versions will keep this field as allways-NULL that can lead to misbehavior if older versions rely on data in this field (anyway you have created it for something, not just for fun :-) )

最明显的是,有时,你改变了你在某些方面的想法。 例如,当你12岁时,你指的是“自行车”,但当你是18岁时,你指的是“摩托车”——如果你的朋友将邀请你“跳过城市”,你将成为唯一骑自行车的人,你将不理解在以下领域保持相同含义的重要性:-

Firstly to answer your question, when we don t declare SerialVersionUID in our class, Java runtime generates it for us, but that process is sensitive to many class meta data including number of fields, type of fields, access modifier of fields, interface implemented by class etc. Therefore it is recommended to declare it ourselves and Eclipse is warning you about the same.

Serialization: We often work with important objects whose state (data in the variables of the object) is so important that we can not risk to lose it due to power/system failures (or) network failures in case of sending the object state to other machine. The solution for this problem is named "Persistence" which simply means persisting (holding/saving) the data. Serialization is one of many other ways to achieve persistence (by saving data to disk/memory). When saving the state of the object, it is important to create an identity for the object, to be able to properly read it back (de-serialization). This unique identification is ID is SerialVersionUID.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...