关于myeclips提示The serializable class XXX does not declare a static final serialVersionUID field of type long的警告

我们在用eclips/myeclips的时候,会出现这个warning,比如在用hibernate时,自动生成表的对应类后,就有这个提示。这是为什么呢?

这与jdk的版本没关系,那是Eclipse提供的功能.
你点它warning的icon两下Eclipse就会自动给定,如果你不喜欢,可以把它关掉,
windows

-> preferences -> compiler -> Error/Warnings -> Potential Programming problems

将Serializable class without serialVersionUID的warning改成ignore.

其实如果你没有考虑到兼容性问题时,那就把它关掉吧.
其实有这个功能是好的.只要任何类别实作了Serializable这个介面,
如果没有加入serialVersionUID,Eclipse都会给你warning提示,
这个serialVersionUID为了让该类别Serializable後兼容.

考虑一下,如果今天你的类Serialized存到硬碟里,
可是後来你却更改了类别的field(增加或减少或改名).
当你Deserialize时,就会出现Exception.这样就会做成不兼容性的问题.

但当serialVersionUID相同时,它就会将不一样的field以type的预设值Deserialize.

这个可以避开不兼容性的问题.

================================================================

【Ohters】

对于“the serializable class XXXXXXXX  does not
declare a static final seriaVersionUID field of type long”的警告

系统一直会提示三种快速解决方案:

1.add default serial version ID

2.add generated serial version ID

3.add "Suppress Warnings"  'servial' to
XXXXXXXX

一、前言

SerialVersionUid,简言之,其目的是序列化对象版本控制,有关各版本反序列化时是否兼容。如果在新版本中这个值修改了,新版本就不兼容旧
版本,反序列化时会抛出InvalidClassException异常。如果修改较小,比如仅仅是增加了一个属性,我们希望向下兼容,老版本的数据都能
保留,那就不用修改;如果我们删除了一个属性,或者更改了类的继承关系,必然不兼容旧数据,这时就应该手动更新版本号,即
SerialVersionUid。

关于其定义,可参考JDK文档:http://download.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html

二、问题

1.如果不显式设置SerialVersionUid,有什么后果?

jdk文档中有解释,建议我们显式声明,因为如果不声明,JVM会为我们自动产生一个值,但这个值和编译器的实现相关,并不稳定,这样就可能在不同JVM环境下出现反序列化时报InvalidClassException异常。

...it is strongly recommended that all serializable classes
explicitly declare serialVersionUID values, since the default
serialVersionUID computation is highly sensitive to class details
that may vary depending on compiler implementations...

2.两种SerialVersionUid有什么区别?

在Eclipse中,提供两种方式让我们快速添加SerialVersionUid。

add default serial version ID:
Adds a default serial version ID to the selected type
Use this option to add a user-defined ID in combination with custom
serialization code if the type did undergo structural change since
its first release.

add generated serial version ID:
Adds a generated serial version ID to the selected type
Use this option to add a compiler-generated ID if the type didnot
undergo structural change since its first release.

一种就是1L,一种是生成一个很大的数,这两种有什么区别呢?

看上去,好像每个类的这个类不同,似乎这个SerialVersionUid在类之间有某种关联。其实不然,两种都可以,从JDK文档也看不出这一点。我们只要保证在同一个类中,不同版本根据兼容需要,是否更改SerialVersionUid即可。

对于第一种,需要了解哪些情况是可兼容的,哪些根本就不兼容。 参考文档:http://java.sun.com/j2se/1.4/pdf/serial-spec.pdf

在可兼容的前提下,可以保留旧版本号,如果不兼容,或者想让它不兼容,就手工递增版本号。

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

第二种方式,是根据类的结构产生的hash值。增减一个属性、方法等,都可能导致这个值产生变化。我想这种方式适用于这样的场景:

开发者认为每次修改类后就需要生成新的版本号,不想向下兼容,操作就是删除原有serialVesionUid声明语句,再自动生成一下。

个人认为,一般采用第一种就行了,简单。第二种能够保证每次更改类结构后改变版本号,但还是要手工去生成,并不是修改了类,会提示你要去更新这个SerialVersionUid,所以虽然看上去很cool,实际上让人很迷惑。

参考:

1.一篇较好的关于serialVesionUid的说明:

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

2.serialVesionUid相关讨论

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

3.compiler-generated ID生成算法

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

其他相关问题:

Hibernate的持久化,这个一般指的是将数据持久化到数据库,和序列化并没有直接关系。

Hibernate的POJO也并不要求必须实现Serializable接口,但是,作为系统扩展考虑,应该把PO都实现Serializable接
口,因为如果这些对象需要缓存到磁盘上,或者在分布式环境下使用,就必须序列化,最常见的例子就是ehcache、Memcached。key和
value中的对象都必须是序列化的对象。

the serializable class XXX does not declare a static final seriaVersionUID...的问题的更多相关文章

  1. 关于The serializable class XXX does not declare a static final serialVersionUID field of type long的警告

    编写实体类并且继承序列化接口时候,实体类会有警告,要生成一个静态的serialVersionUID. 上网搜了一下资料,现通俗解释一下: 点击前2个选项,会生成: private static fin ...

  2. The serializable class XXX does not declare a static final serialVersionUID field of type long

    问题: 在Eclipse中,继承类时,总是提示下面的警告(Warning),按理说警告是没有关系的,但是程序看起来老不爽了,这是强迫症的关系(呵呵) The serializable class XX ...

  3. Eclipse警告:The serializable class XXX does not declare a static final serialVersionUID field of type long

    serialVersionUID作用: 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性. 在Eclipse中可以自动生成,有两种生成方式: 一个是默认的1L,比如:privat ...

  4. The serializable class XXX does not declare a static final serialVersionUID field of type long的警告

    原文: http://blog.csdn.net/ultrakang/article/details/41820543

  5. Eclipse下关于The serializable class UsersServlet does not declare a static final serialVersionUID field of type的警告

    The serializable class XXX does not declare a static final serialVersionUID field of type long seria ...

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

    在编译以下Java程序时,出现The serializable class  does not declare a static final serialVersionUID field of typ ...

  7. 【转载】Understand the serialVersionUID

    If you have ever implemented Serializable interface, you must encounter this warning message The ser ...

  8. private static final long serialVersionUID = 1L;详解

    public class User implements Serializable { /** * serialVersionUID */ private static final long seri ...

  9. spring 注解列表

    table th:first-of-type { width: 15%; } table th:nth-of-type(2) { } 注解 作用 例子 @SuppressWarnings 忽略警告 类 ...

随机推荐

  1. GATT 服务器与客户端角色

    两个设备应用数据的通信是通过协议栈的GATT层实现的.从GATT角度来看,当两个设备建立连接后,他们处于以下两种角色之一: GATT服务器: 它是为GATT客户端提供数据服务的设备 GATT客户端:  ...

  2. 分布式中,zookeeper的部署

    一:准备 1.概述 为分布式应用提供协调服务的项目 类似于文件系统那样的树形数据结构 目的:将分布式服务不再由于协作冲突而另外实现协作服务 2.数据结构 树形数据结构 zookeeper的每个节点都是 ...

  3. H264(NAL简介与I帧判断)

    1.NAL全称Network Abstract Layer, 即网络抽象层.         在H.264/AVC视频编码标准中,整个系统框架被分为了两个层面:视频编码层面(VCL)和网络抽象层面(N ...

  4. jade模板引擎的基本使用

    jade和ejs很大的不同是通过缩进的方式解决嵌套和成对标签的问题,比较适合有代码洁癖的同学. jade官方有基础的语法: http://naltatis.github.io/jade-syntax- ...

  5. 各个JSON技术的比较

    JSON技术的调研报告 一 .各个JSON技术的简介和优劣1.json-libjson-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确实是依赖于很多第三方包,包括com ...

  6. css实现三角箭头

    像下面的向右三角箭头,只有纯css不需要图片就可以实现了. width:0px;height:0px;border-width:0px 16px 20px 16px; border-style:sol ...

  7. Mac 系统下将普通文件变为可执行文件

    在使用Cocospods时,老是提示我pod文件里面有文稿的东西.后来想到前同事将他变为可执行文件了.所以百度了一下,方法如下: chmod +x (此处是文件的地址) 就可以了

  8. imx6 生成 spi设备节点

    开发板需要使用spi接口,但是spi接口被touch占用,使用event进行操作.所以需要更改配置,生成spi设备节点. 参考链接 https://community.nxp.com/thread/3 ...

  9. ClassLoader

    1.双亲委派制 ClassLoadder是一个abstract类 static class sun.misc.Launcher$ExtClassLoader extends java.net.URLC ...

  10. each的详解

    首先我还是先观察w3c讲解: 先写一段代码,如图: 定义:each() 方法规定为每个匹配元素规定运行的函数. 提示:返回 false 可用于及早停止循环.(我在代码中加了return false,发 ...