I've already asked this question on the Hibernate's forum, but I thought I'd ask it here too.
I'm trying to map the following model while preserving the value semantics of the TranslatedText
and Translation
value objects:
Both values as dependent objects
Ideally I'd map TranslatedText
as a <component>
within Question
and Translation
as a <bag>
of <composite-element>
within TranslatedText
.
It would have been simple to map if Question
was only referencing one TranslatedText
, but since it references two I need some kind of discriminator based on the name of the property holding the value (title
or description
) in order to map the Translation
with a foreing key composed of (question_id,property_name,language_code)
.
One problem with that is that the propertyName
isin't part of the model and shouldn't, but I haven't found a way to force Hibernate to insert a value that doesn't come from the model.
Therefore, I tried to change the model and introduce specialized Title
and Description
classes so that I'd have a type
in there that I could use as a discriminator.
At the end that did not really help much:
<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>
TranslatedText with <many-to-one>
I managed to get something close to what I need by mapping TranslatedText
as an entity within Question
using a <many-to-one>
and then map Translation
as a collection of values within TranslatedText
, but the main problem with that approach is that there is no easy way to get rid of the orphaned TranslatedText
and Translation
. I'd either have to do this with a DB trigger or a scheduled process.
Conclusion
At this point I'm under the impression that Hibernate is not flexible enough to map the initial model with the proper semantics, but hopefully I'm wrong and there is a way to do it.