Hibernate Annotations indicating which component elements should create persistent

I have a Price object consisting of two MonetaryValues, where one MonetaryValue consists of a sum and a currency.

If I configure OR-mapping XML-way, I can do it

<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 save the baseCurrency element (since it is implicit and always the same).

Can this be achieved in the form of an annotation configuration?


If I just do this and ignore the baseCurrency attribute, it will be saved in any case with the 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 is also impossible to make the currency of the MonetaryValue property transitional, since then it will never be saved.

So, is it possible to achieve what the above XML mapping does through annotations?


Just as mtpettyp suggests, I want to save two MonetaryValue files in a table using only three columns. As the autocracy suggests in your comment, you could solve the problem of inheritance. But then again, you can also solve it using a custom .hbm.xml-mapping file instead of using annotations. I am not sure which is more correct, but I am still wondering if it can be solved with either ...

+4
source share
1 answer

I'm still confused by your question, but am I going to answer with the assumption that you are trying to read baseCurrency without even updating it?

 // Use this in the override statement for your first baseCurrency @Column(insertable=false,updatable=false) 

Result:

 @Embedded @AttributeOverrides ( { @AttributeOverride(name="amount", column= @Column(name="baseAmount")) @AttributeOverride(name="currency", column= @Column(name="baseCurrency", insertable=false,updatable=false)) } ) private MonetaryValue baseAmount; @Embedded @AttributeOverrides ( { @AttributeOverride(name="amount", column= @Column(name="originalAmount")), @AttributeOverride(name="currency", column= @Column(name="originalCurrency")) } ) private MonetaryValue originalAmount; 

However, you should clarify if this is not what you mean. I really can't say what you are trying to do here.

+3
source

All Articles