【发布时间】:2017-03-11 15:56:06
【问题描述】:
我已经在Hibernate's forum 上问过这个问题,但我想我也会在这里问。
我正在尝试映射以下模型,同时保留 TranslatedText 和 Translation 值对象的值语义:
作为依赖对象的两个值
理想情况下,我会将TranslatedText 映射为Question 内的<component>,并将Translation 映射为<composite-element> 内的<bag> TranslatedText。
如果Question 只引用一个TranslatedText,映射会很简单,但由于它引用了两个,我需要某种基于持有值的属性名称的鉴别器(title 或@987654338 @) 以便将Translation 映射到由(question_id,property_name,language_code) 组成的外键。
其中一个问题是 propertyName 不是模型的一部分,也不应该,但我还没有找到一种方法来强制 Hibernate 插入一个不是来自模型的值。
因此,我尝试更改模型并引入专门的 Title 和 Description 类,以便在其中有一个 type 可以用作鉴别器。
最后并没有太大帮助:
<component name="title" class="TranslatedText">
<bag name="translations" table="Translation">
<key>
<!-- PROBLEM: Could not find a way to create a custom join expression on question.id and question.title.type in here. -->
</key>
<composite-element class="Translation">
<!-- PROBLEM: Could not found a way to make Hibernate insert title.type from here, without having this value on the Translation object. -->
<property name="languageCode" type="string" column="language_code"/>
<property name="text" type="string"/>
</composite-element>
</bag>
</component>
带有<many-to-one>的翻译文本
通过使用<many-to-one> 将TranslatedText 映射为Question 中的实体,然后将Translation 映射为TranslatedText 中的值集合,我设法得到了接近我需要的东西,但主要问题采用这种方法是没有简单的方法可以摆脱孤立的TranslatedText 和Translation。我要么必须使用数据库触发器或计划进程来执行此操作。
结论
在这一点上,我的印象是 Hibernate 不够灵活,无法使用正确的语义映射初始模型,但希望我错了,有办法做到这一点。
【问题讨论】:
标签: java hibernate domain-driven-design hibernate-xml-mapping