关于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. ECharts SSH+JQueryAjax+Json+JSP将数据库中数据填充到ECharts中

    本文引用自:http://blog.csdn.net/ArcticFoxHan/article/details/38071641   1.导入包,搭建SSH框架 导入Jquery的JS包,<sc ...

  2. sqlserver快速查找所有存储过程中是否包含某字符

    --将text替换成你要查找的内容 select name from sysobjects o, syscomments s where o.id = s.id and text like '%tex ...

  3. iOS Provisioning Profile(Certificate)与Code Signing详解

    引言 关于开发证书配置(Certificates & Identifiers & Provisioning Profiles),相信做 iOS 开发的同学没少被折腾.对于一个 iOS ...

  4. cssHack

    cssHack 不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不 ...

  5. iOS:消除项目中警告

    引言: 在iOS开发过程中, 我们可能会碰到一些系统方法弃用, weak.循环引用.不能执行之类的警告. 有代码洁癖的孩子们很想消除他们, 今天就让我们来一次Fuck 警告!! 首先学会基本的语句: ...

  6. linux 入门

    php            php -v ------------------ 阿帕奇 apachectl -v httpd -v ----------------------------- mys ...

  7. php 扩展dll

    一.准备工作: 注:php5.2没有vc9,php5.3.php5.4没有vc6.呵呵.PHP5.5开始,不支持xp和win2003了,更是vc11了.--------------->所以,扩展 ...

  8. 面向对象编程(七)——Static关键字

    Static变量

  9. 第六篇 SQL Server安全执行上下文和代码签名

    本篇文章是SQL Server安全系列的第六篇,详细内容请参考原文. SQL Server决定主体是否有必要的执行代码权限的根本途径是其执行上下文规则.这一切都可能复杂一个主体有执行代码的权限,但是却 ...

  10. 无法定位程序输入点 crtis tailoredApp 于动态链接库MSVCR110.dll

    最近 在安装完   PHP开发,集成环境 WAMP  后,运行老是提示 “无法定位程序输入点 crtis tailoredApp 于动态链接库MSVCR110.dll”  网上百度,大多说是重新下载“ ...