Hi,
I am trying to map an entity with two embedded properties of the same type, using annotations. This works as long as the properties of the embedded entity are not a reference to another entity.
My example:
Class Amount
Code:
@Embeddable
public class Amount
{
private double amount;
private String currencyCode;
...
}
Class AccountCode:
@Entity
public class Account
{
@Id
private Long id;
private String accountName;
@Embedded
private Amount creditLimit;
@Embedded
private Amount availableCredit;
...
}
This does not work, as Hibernate tries to map both creditLimit.amount and availableCredit.amount to the same column amount in the generated SQL table.
Ok, we need to change the annotations:
Class Account, 2nd versionCode:
@Entity
public class Account
{
@Id
private Long id;
private String accountName;
@Embedded
@AttributeOverrides( {
@AttributeOverride(name="amount", column = @Column(name="creditLimit_amount") ),
@AttributeOverride(name="currencyCode", column = @Column(name="creditLimit_currencyCode") )
} )
private Amount creditLimit;
@Embedded
@AttributeOverrides( {
@AttributeOverride(name="amount", column = @Column(name="availableCredit_amount") ),
@AttributeOverride(name="currencyCode", column = @Column(name="availableCredit_currencyCode") )
} )
private Amount availableCredit;
...
}
This works nicely and generates a table with the following columns:
Code:
id
accountName
creditLimit_amount
creditLimit_currencyCode
availableCredit_amount
availableCredit_currencyCode
My first question here: is there a way to make Hibernate automatically prefix the the column name with the property name of the embedded object (so we do not need to write this ugly @AttributeOverrides construct)?
But of course our Amount class does not store a currencyCode, but a reference to a Currency object:
Class CurrencyCode:
@Entity
public class Currency
{
@Id
private Long id;
private String code;
private String name;
private int precision;
...
}
Class Amount, 2nd versionCode:
@Embeddable
public class Amount
{
private double amount;
@ManyToOne
private Currency currency;
...
}
Now I don't find a way of telling Hibernate to use creditLimit_currency_id for the reference to property creditLimit.currency. Both
Code:
...
@Embedded
@AttributeOverrides( {
@AttributeOverride(name="amount", column = @Column(name="creditLimit_amount") ),
@AttributeOverride(name="currency", column = @Column(name="creditLimit_currency") )
} )
...
and
Code:
...
@Embedded
@AttributeOverrides( {
@AttributeOverride(name="amount", column = @Column(name="creditLimit_amount") ),
@AttributeOverride(name="currency_id", column = @Column(name="creditLimit_currency_id") )
} )
...
are simply ignored. When Hibernate tries to build my database schema, the first Amount creates a table column currency_id, and the second Amount (avaliableCredit) tries to create the same column, resulting in the exception:
Code:
org.hibernate.MappingException: Repeated column in mapping for entity: Account column: currency_id (should be mapped with insert="false" update="false")
Any hints on this?
Best Regards,
Christoph Jäger