将 Hibernate Entity 转换为清理 POJO 以进行序列化
我对在 Java 中使用 ORM 东西还是很陌生,这里有一个问题我似乎一直困扰着:
我有大量的 Hibernate 实体,想要在服务器上查询它们(工作正常),然后序列化它们(使用ObjectOuputStream)并将它们发送到客户端。
如果我在客户端反序列化它们,仍然存在大量 Hibernate / javax.persistence 依赖项。
是否有可能以某种方式将我的实体转换为 POJO,而不依赖于 hibernate?
谢谢!
编辑: 为了澄清这一点,我正在使用注释,这可能很愚蠢。我需要将所有类重新定义为无注释的,那么它们应该是标准的 POJO。
I'm still new to using ORM stuff in Java, and here is one question I seem to be stuck on:
I have a large number of Hibernate Entities and want to query for them on the server (works fine) then serialise them (using ObjectOuputStream) and send them to a client.
If I deserialize them on the client there is still a large number of Hibernate / javax.persistence dependencies.
Is it possible to transform my entities to POJOs with no dependency on hibernate at all somehow?
Thanks!
EDIT:
To clear thing up, I am using annotations, which is probably silly. I will need to redefine all classes to be annotation-free, then they should be standard POJOs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您只需回退到 XML 配置,而不是方便的注释。 Hibernate 有其
hbm
< /a> 从一开始就创建文件,对于 JPA,您可以使用orm.xml
。一切都可以用XML来表达,注释只是语法糖。这样,您的实体将完全不受 Hibernate/
javax.persistence
引用的影响。另请参阅:JPA:我应该使用 orm.xml 清理我的实体类吗?
PS:请记住序列化您的域模型(一旦您开始重构域模型,JPA/Hibernate 实体)很快就会被证明是一种痛苦。即使你控制了双方。真的,真的考虑 DTO。
Yes, you just have to fall back to XML configuration as opposed to convenient annotations. Hibernate has its
hbm
files from the very beginning, with JPA you useorm.xml
.Every can be expressed with XML, annotations are only syntactic sugar. This way your entities will be absolutely free of Hibernate/
javax.persistence
references.See also: JPA: Should I clean up my entity classes using orm.xml?
P.S.: Keep in mind that serializing your domain model (JPA/Hibernate entities) will prove to be a pain soon once you start refactoring your domain model. Even when you control both sides. Really, really consider DTOs.