2

我以前看过一个编码最佳实践是hibernate的实体不应该实现

java.io.Serializable

.

我的想法只是为了获得更好的性能,以避免编译器为实体处理某些内容。我们应该使用 DTO 序列化来代替客户端和服务器进行通信。

这样对吗?

非常感谢!

4

2 回答 2

0

Hibernate POJO 不可序列化。这不是最佳实践或性能的问题。它根本行不通。这就是为什么。

将 POJO 声明为可序列化时不会出现任何编译器错误。但是在执行时,当您尝试序列化和反序列化对象时,您可能会遇到异常。

例如,当您使用 GWT 并在客户端和服务器之间发送数据时,就会出现问题。这在 Hibernate POJO 中失败,只是因为它们不能被序列化。有关更详细的异常,请参阅“为什么当 Hibernate 对象到达浏览器世界时无法理解它们”一章中对 GWT 与 Hibernate 的描述(在页面中间之前)。

于 2013-05-06T07:29:03.433 回答
0

That's not a best practice at all, and implementing serializable or not doesn't have any impact on performance. If you want to be able to serialize your entities, make them implement Serializable. If you don't want to, then don't make them implement Serializable. It's as simple as that.

Using a DTO when the entity itself contains the adequate information is rather viewed as an anti-pattern than viewed as a best practice.

于 2013-05-05T17:32:29.800 回答