My FAQ,最新最全的IT技术FAQ
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 未整理篇 | 技术讨论
  当前位置: > 程序开发 > 编程语言 > Java > 综合文章
如何生成serialVersionID?
作者:未知 时间:2005-09-13 19:40 出处:ChinaUnix.net 责编:My FAQ
              摘要:如何生成serialVersionID?

我用eclipse自动生成总是出错,不知道哪里不对了。听说用JDK和什么工具能够生成,请问知道的达人提供些详细情况。。。

 icesummit 回复于:2005-08-19 23:24:14
自己顶一下先。

 wolfg 回复于:2005-08-19 23:59:26
试试这个生成serialVersionUID的插件
http://eclipse-plugins.2y.net/eclipse/plugin_details.jsp?id=257

 icesummit 回复于:2005-08-20 11:34:19
多谢wolfg,我待会试试。我看到很多人直接用eclipse就可以成功生成serialVersionUID,但是我一做,就是一个1L。不知道有什么配置上的问题。

我先试试这个插件。

 快乐的土豆 回复于:2005-08-20 14:35:52
小心滴问一下,这个id做什么用滴? :oops:  :oops:

 wolfg 回复于:2005-08-21 00:09:56
[quote:a44d6dee8f="快乐的土豆"]小心滴问一下,这个id做什么用滴? :oops:  :oops:[/quote:a44d6dee8f]

The serialVersionUID is a universal version identifier for a Serializable class. Deserialization uses this number to ensure that a loaded class corresponds exactly to a serialized object. If no match is found, then an InvalidClassException is thrown.

Guidelines for serialVersionUID :

    * always include it as a field, for example: "private static final long serialVersionUID = 7526472295622776147L; " include this field even in the first version of the class, as a reminder of its importance
    * do not change the value of this field in future versions, unless you are knowingly making changes to the class which will render it incompatible with old serialized objects
    * new versions of Serializable classes may or may not be able to read old serialized objects;  it depends upon the nature of the change; provide a pointer to Sun's guidelines for what constitutes a compatible change, as a convenience to future maintainers

Look this for details
http://www.javapractices.com/Topic45.cjp

 wolfg 回复于:2005-08-21 00:32:50
[quote:9ba6db6ce4="icesummit"]多谢wolfg,我待会试试。我看到很多人直接用eclipse就可以成功生成serialVersionUID,但是我一做,就是一个1L。不知道有什么配置上的问题。

我先试试这个插件。[/quote:9ba6db6ce4]

其实不用插件也可以生成。新加一个implements Serializable的类,编辑器左边会有一个警告标记(灯泡加叹号),双击它,会有一个菜单弹出,可以选择生成默认的serialVersionUID(1L),还是随机的。

 icesummit 回复于:2005-08-21 08:20:58
[quote:5e7b975829="wolfg"]

其实不用插件也可以生成。新加一个implements Serializable的类,编辑器左边会有一个警告标记(灯泡加叹号),双击它,会有一个菜单弹出,可以选择生成默认的serialVersionUID(1L),还是随机的。[/quote:5e7b975829]

但是默认生成的1L不行吧?这么短恐怕不行呀。

 wolfg 回复于:2005-08-21 12:34:01
[quote:c7b8962266="icesummit"]

但是默认生成的1L不行吧?这么短恐怕不行呀。[/quote:c7b8962266]

抱歉,上面写错了,应该是“还有一个随机的”,见图。default就是1L,generated就是随机的






 icesummit 回复于:2005-08-21 19:45:40
偶用的就是generated,但是仍然是1L。

 yovn 回复于:2005-08-23 21:47:34
[quote:002399fc2a="wolfg"]
抱歉,上面写错了,应该是“还有一个随机的”,见图。default就是1L,generated就是随机的[/quote:002399fc2a]

这个应该不是随机的,是根据Class Name,Filed,Method等等算的Hash值,具体的算法,可以看Java Serializable 规范。
选择Default,1L也是可以的,不是只根据这个字段来唯一标识一个Class的。

 wolfg 回复于:2005-08-23 22:41:26
[quote:4611655dec="yovn"]

这个应该不是随机的,是根据Class Name,Filed,Method等等算的Hash值,具体的算法,可以看Java Serializable 规范。
选择Default,1L也是可以的,不是只根据这个字段来唯一标识一个Class的。[/quote:4611655dec]多谢指教! :)

 cooljia 回复于:2005-08-24 00:44:14
A good way to solve many problem with object that are Serializable (EJB
implementation are) is to add the serialVerUID in every Serializable class. To add
it, compile your class, then run serialVer with your fully qualified class name as
an argument and it will print the line to add to your class.
Add that line to the class and recompile. Now when you modify the class, it will
have the same serialVerUID and it will cause less error at runtime. This is
notdangerous to do, because if a new version of a class in not compatible with an
old one, unserializing it will still cause an exception when tring to assign
incompatible fields.


1. serialVer.exe这个工具可以在jdk的目录下查找到.
2. 基本上加入类的这个成员变量将会看起来是这个样子的static final long serialVersionUID = -2199636203015991542L;

 cooljia 回复于:2005-08-24 00:45:20
有个问题, wolfg, 在我的eclipse3.0中, implements Serializable并不能带来你那个小灯泡, 是不是RPWT啊?

 sakulagi 回复于:2005-08-24 08:42:48
试试Eclipse 3.1没准就可以了。肯定不是RPWT,hoho

 dennis2 回复于:2005-08-28 15:03:45
> 在我的eclipse3.0中, implements Serializable并不能带来你那个小灯泡, 是不是RPWT啊?

(这个 ... 不好意思,什么是 RPWT 啊?)

serialVerUID 只是在 JDK 5 中才是强迫使用的,而 eclipse 只是在 3.1 才支持 5,所以在 3.0 里面没有错误提示。

 sakulagi 回复于:2005-08-28 17:32:02
RPWT == Ren Pin Wen Ti (人品问题,类似于“点背不能怨社会,命苦不能赖政府”的说法)

 goncha 回复于:2005-08-28 20:27:58
是否把未声明searialVersionID做为Error是可以在Java相关的属性里做设置的.

 goncha 回复于:2005-08-28 20:32:40
不知道自动生成的serialVersionID会不会根据类中持久化Field的增减自动做调整, 不然serialVersionID不过是个摆设.

 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 myfaq.com.cn All rights reserved. www.myfaq.com.cn 版权所有