Java Hibernate 表类是否需要可序列化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2726300/
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-08-13 12:10:31  来源:igfitidea点击:

Do Hibernate table classes need to be Serializable?

javahibernateormserializable

提问by Scott Leis

I have inherited a Websphere Portal project that uses Hibernate 3.0 to connect to a SQL Server database.

我继承了一个使用 Hibernate 3.0 连接到 SQL Server 数据库的 Websphere Portal 项目。

There are about 130 Hibernate table classes in this project. They all implement Serializable. None of them declare a serialVersionUID field, so the Eclipse IDE shows a warning for all of these classes.

这个项目中大约有 130 个 Hibernate 表类。它们都实现了可序列化。它们都没有声明 serialVersionUID 字段,因此 Eclipse IDE 会显示所有这些类的警告。

Is there any actual need for these classes to implement Serializable?
If so, is there any tool to add a generated serialVersionUID field to a large number of classes at once (just to make the warnings go away) ?

这些类是否真的需要实现可序列化?
如果是这样,是否有任何工具可以一次将生成的 serialVersionUID 字段添加到大量类中(只是为了使警告消失)?

采纳答案by Pascal Thivent

Is there any actual need for these classes to implement Serializable?

这些类是否真的需要实现可序列化?

The JPA spec (JSR 220) summarizes it pretty well (the same applies to Hibernate):

JPA 规范 (JSR 220) 对其进行了很好的总结(这同样适用于 Hibernate):

2.1 Requirements on the Entity Class

(...)

If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the entity class must implement the Serializableinterface.

2.1 实体类要求

(……)

如果实体实例要作为分离对象按值传递(例如,通过远程接口),则实体类必须实现该Serializable接口。

So, strictly speaking, this is not a requirement unless you need detached entities to be sent over the wire to another tier, to be migrated to another cluster node, to be stored in the HTTP session, etc.

因此,严格来说,这不是必需的,除非您需要将分离的实体通过线路发送到另一层、迁移到另一个集群节点、存储在 HTTP 会话中等。

If so, is there any tool to add a generated serialVersionUID field to a large number of classes at once

如果是这样,有没有什么工具可以一次性将生成的serialVersionUID字段添加到大量的类中

I think that you could batch this with the Serial version (Ant) Tasks.

我认为您可以使用Serial version (Ant) Tasks来批量处理。

回答by Uri

I'm not familiar with a tool that would do it automatically for a bunch of classes, though you could write a script.

我不熟悉可以为一堆类自动执行此操作的工具,尽管您可以编写脚本。

If you mechanically do it with Eclipse, it shouldn't take you more than 4-5 seconds per class which might still be faster than a script.

如果你用 Eclipse 机械地做这件事,每节课不应该超过 4-5 秒,这可能仍然比脚本快。

回答by Chris Nava

Eclipse's "Clean Up" command can do that. However, it also does a lot more so test it and refine the cleanup and format settings before using it on an entire package.

Eclipse 的“清理”命令可以做到这一点。但是,它还可以做更多的工作,因此在将其用于整个包之前对其进行测试并优化清理和格式设置。

回答by bmargulies

Hibernate does not require serializable at all.

Hibernate 根本不需要可序列化。

回答by matt b

If so, is there any tool to add a generated serialVersionUID field to a large number of classes at once (just to make the warnings go away) ?

如果是这样,是否有任何工具可以一次将生成的 serialVersionUID 字段添加到大量类中(只是为了使警告消失)?

You can disable this warning within the Compiler settings for Eclipse - either for the project or your workspace as a whole.

您可以在 Eclipse 的编译器设置中禁用此警告 - 无论是针对项目还是针对整个工作区。