【问题标题】:Why while using Spring Boot I need entities to be serializable?为什么在使用 Spring Boot 时我需要实体可序列化?
【发布时间】:2018-08-06 19:38:59
【问题描述】:
简单的 Spring Boot 实体 CRUD 操作工作,不实现 serializable 接口。几乎每个示例和教程都没有这样做,但这些示例仍然运行良好。但是official simple code samples 使用它。
为什么需要这个接口?实现serializable接口时最简单的示例用例是什么?
【问题讨论】:
标签:
spring
hibernate
spring-boot
spring-data
spring-data-jpa
【解决方案1】:
您不必这样做。但是看看 JPA JSR-220 规范,有些情况是你需要的。来自规范:
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.
因此,如果您通过 RMI 传递对象,那么您将需要实现 Serializable。我还可以想象,如果您将实体放在 Session 中,可能需要 Serializable ,这可能会在服务器关闭时序列化到磁盘。