与 XML 映射文件等效的 Hibernate @embeddable 注释? [英] Hibernate @embeddable annotation equivalent for XML mapping file?
问题描述
我有一个类,我正在创建一个 Hibernate 映射,其中包含一个我无法修改的遗留对象,因此它没有必要的 id 字段来很好地与 Hibernate 配合使用.我想将遗留对象注释为我的新类的 @Embedded 字段,并为遗留对象编写一个 hbm.xml 文件,并注意它是可嵌入的.有没有办法做到这一点?我见过的唯一关于嵌入对象的文档是指注释对象而不是使用 XML.
I have a class that I am creating a Hibernate mapping which contains a legacy object that I can't modify, so it doesn't have the necessary id field to play nicely with Hibernate. I would like to annotate the legacy object as an @Embedded field of my new class and write an hbm.xml file for the legacy object and note that it is embeddable. Is there a way to do this? The only documentation for embedding objects I've seen refers to annotating objects instead of using XML.
我意识到我可以扩展遗留对象并对其进行适当的注释,但这些情况可能会经常发生,所以我想尽可能避免这种情况.
I realize that I could extend the legacy object and annotate it appropriately, but these case might occur frequently so I'd like to avoid that if possible.
推荐答案
@Embedded
的 XML 对应物是
,见 5.1.5.嵌入对象(又名组件).
The XML counterpart of @Embedded
is <component>
, see 5.1.5. Embedded objects (aka components).
但是,它与@Embeddable
/@Embedded
对的工作方式不同,您需要在 中描述组件类的所有属性.hbm.xml
包含类,类似这样:
However, it doesn't work the same way as the @Embeddable
/@Embedded
pair, you need to describe all properties of the component class in .hbm.xml
of the containing class, something like this:
<class name = "NewClass">
...
<component name = "legacyObject">
... properties of the legacy class ...
</component>
</class>
这篇关于与 XML 映射文件等效的 Hibernate @embeddable 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!