您好,我在使用Hibernate和@AttributeOverrides批注时遇到问题。

我有可嵌入的班级钱

@Embeddable
 public class Money implements Serializable {

     private BigDecimal denomination;

     private String currencyCode;

 }


而实体贷款则包含两个货币对象:价值与利息

 @Entity
 public class Loan {

     @AttributeOverrides({
             @AttributeOverride(name = "valuedenomination", column = @Column(name = "valuedenomination")),
             @AttributeOverride(name = "valuecurrencycode", column = @Column(name = "valuecurrencycode")) })
     private Money value;

    @AttributeOverrides({
             @AttributeOverride(name = "interestdenomination", column = @Column(name = "interestdenomination")),
             @AttributeOverride(name = "interestcurrencycode", column = @Column(name = "interestcurrencycode")) })
     private Money interest;

 }


当我尝试打包应用程序时,出现以下原因异常:

由以下原因引起:org.hibernate.MappingException:实体映射中的重复列:org.finance.app.core.domain.common.loan.Loan列:currencyCode(应与insert =“ false” update =“ false”映射)

当我从数据库的Loam中删除One Money实体时,我得到了两个字段:
CurrencyCode和面额而不是我在AttributeOverrides中输入的valuecurrencyCode和valuedenomination。

我会错过带有AttributeOverrides批注的内容吗?

最佳答案

AttributeOverride::name应该是嵌入式类Money中属性的名称。因此,您的映射应如下所示:

 @Entity
 public class Loan {

     @AttributeOverrides({
             @AttributeOverride(name = "denomination",
                                column = @Column(name = "valuedenomination")),
             @AttributeOverride(name = "currencyCode",
                                column = @Column(name = "valuecurrencycode")) })
     private Money value;

    @AttributeOverrides({
             @AttributeOverride(name = "denomination",
                                column = @Column(name = "interestdenomination")),
             @AttributeOverride(name = "currencyCode",
                                column = @Column(name = "interestcurrencycode")) })
     private Money interest;

 }

10-05 23:33