I have a Price object consisting of two MonetaryValues where one MonetaryValue consists of an amount and a currency.
If I configure the OR-mapping the XML-way, I can do this
<component name="baseAmount" lazy="false" class="MonetartyValue">
<property name="amount" column="baseAmount" precision="20" scale="2" not-null="true" />
<!-- <property name="currency" column="baseCurrency" not-null="true" /> -->
</component>
<component name="originalAmount" lazy="false" class="MonetaryValue">
<property name="amount" column="originalAmount" precision="20" scale="2" not-null="true" />
<property name="currency" column="originalCurrency" not-null="true" />
</component>
i.e. choose not to persist the baseCurrency element (since it is implicit and always the same).
Is it possible to achieve this in an annotation-configuration manner?
If I just do like this, and leave out the baseCurrency attribute, it will be persisted anyway, with a default name.
@Embedded
@AttributeOverrides ( {
@AttributeOverride(name="amount", column= @Column(name="baseAmount"))
} )
private MonetaryValue baseAmount;
@Embedded
@AttributeOverrides ( {
@AttributeOverride(name="amount", column= @Column(name="originalAmount")),
@AttributeOverride(name="currency", column= @Column(name="originalCurrency"))
} )
private MonetaryValue originalAmount;
It also not possible to make the property currency of MonetaryValue transient, since then it will never be saved.
So, is it possible to achieve what the above XML-mapping does, by means of annotations?
Just as mtpettyp suggests, I want to store two MonetaryValue in a a table, using only three columns. As Autocracy suggests in his comment, you could definitely solve the problem with inheritance. But then again, you can also solve it with with a custom .hbm.xml-mapping file instead of using annotations. I am not certain which is more correct, but I'm still curious if it's possible to solve with neither...