我继承了一个使用 Hibernate 3.0 连接到 SQL Server 数据库的 Websphere Portal 项目。
这个项目中大约有 130 个 Hibernate 表类。它们都实现了可序列化。它们都没有声明 serialVersionUID 字段,因此 Eclipse IDE 显示所有这些类的警告。
这些类是否真的需要实现 Serializable?
如果是这样,是否有任何工具可以一次将生成的 serialVersionUID 字段添加到大量类(只是为了使警告消失)?
最佳答案
Is there any actual need for these classes to implement Serializable?
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
Serializable
interface.
因此,严格来说,这不是必需的,除非您需要将分离的实体通过线路发送到另一层、迁移到另一个集群节点、存储在 HTTP session 中等。
If so, is there any tool to add a generated serialVersionUID field to a large number of classes at once
我认为您可以使用 Serial version (Ant) Tasks 进行批处理.
关于java - Hibernate 表类是否需要可序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2726300/