views:

810

answers:

1

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...

+2  A: 

I'm still confused by your question, but I'm going to answer with a guess that you are trying to read baseCurrency without ever updating it?

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

Resulting in:

@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;

You should clarify more, though, if that's not what you mean. I really can't tell what you're trying to do here.

Autocracy
He's trying to embed two MonetaryValue objects in his entity but only have three columns persisted - baseAmount (from the baseAmount field) and originalAmount and originalCurrency (from originalAmount field)
mtpettyp
Well that just makes it sound like he'd be better off with multiple classes and inheritance. Make one class for persistence, two classes that interface / represent it the way you want to see it? I still don't quite get the implementation...
Autocracy
mtpettyp is right about what I'm after. I simply want to rid the baseCurrency information from the database, since it's a system wide default and constant. Is inheritance the best way to solve this? "MonetaryValue" and "FixedCurrencyMonetaryValue extends MoneteryValue"? Or the other way around?
Sebastian Ganslandt
Also, thanks for honest tries of answering the question : )
Sebastian Ganslandt
Yeah, I'd say do exactly that. Then you can have it hardcoded / from a config in one, and persisted / from the database in the other, yet access them the same way.
Autocracy