嵌套嵌入式 - AttributeOverride,用于嵌入嵌入式嵌入式

时间:2012-03-23 19:44:27

标签: hibernate jpa embeddable

我有类Money,这是一个@Embeddable

@Embeddable
public class Money implements Serializable, Comparable<Money> {
    @Column(name = "amount", precision = 15, scale = 2)
    private BigDecimal amount;
}

当我在实体内部使用多次时,一切正常。例如

@Entity
public class SomeEntity implements Serializable {

    @Embedded
    @AttributeOverride(name = "amount", column = @Column(name = "entry"))
    private Money entryValue;

    @Embedded
    @AttributeOverride(name = "amount", column = @Column(name = "leave"))
    private Money leaveValue;
}

上面的代码完美无缺。

现在问题出现了,当我有另一个@Embeddable,我希望在其中有Money实例,并且实体多次使用@Embeddable。例如:

  1. 嵌入式

    @Embeddable
    public class ReportCostValues implements Serializable {
    
        @Embedded
        @AttributeOverride(name = "amount", column = @Column(name = "covered_by_grant"))
        private Money coveredByGrant;
    
        @Embedded
        @AttributeOverride(name = "amount", column = @Column(name = "own_resources"))
        private Money foundedFromOwnResources;
    
        @Embedded
        @AttributeOverride(name = "amount", column = @Column(name = "personal_contribution"))
        private Money personalContribution;
    
  2. 实体

     @Entity
     public class ReportCostEntity implements Identifiable<Long>, Serializable {
    
        @Id
        private Long id;
    
        @Embedded       
        private ReportCostValues contracted;
    
        @Embedded       
        private ReportCostValues current;
    
        @Embedded        
        private ReportCostValues previousReport;
    
  3. 上面的代码不起作用。任何想法如何处理这个问题?

1 个答案:

答案 0 :(得分:29)

嗨,你必须在司法上使用@AttributeOverrides,你必须在你在embeddable ReportCostValues类中完成的实体中再次覆盖属性,希望下面的代码是你正在寻找的。

@Entity 
public class ReportCostEntity implements  Serializable {

    @Id
    private Long id;

    @Embedded   
    @AttributeOverrides( {
        @AttributeOverride(name="coveredByGrant.amount", column = @Column(name="contracted_coveredByGrant") ),
        @AttributeOverride(name="foundedFromOwnResources.amount", column = @Column(name="contracted_foundedFromOwnResources")),
        @AttributeOverride(name="personalContribution.amount", column = @Column(name="contracted_personalContribution"))
    } )
    private ReportCostValues contracted;

    @Embedded
    @AttributeOverrides( {
        @AttributeOverride(name="coveredByGrant.amount", column = @Column(name="current_coveredByGrant") ),
        @AttributeOverride(name="foundedFromOwnResources.amount", column = @Column(name="current_foundedFromOwnResources")),
        @AttributeOverride(name="personalContribution.amount", column = @Column(name="current_personalContribution"))
    } )
    private ReportCostValues current;

    @Embedded 
    @AttributeOverrides( {
        @AttributeOverride(name="coveredByGrant.amount", column = @Column(name="previousReport_coveredByGrant") ),
        @AttributeOverride(name="foundedFromOwnResources.amount", column = @Column(name="previousReport_foundedFromOwnResources")),
        @AttributeOverride(name="personalContribution.amount", column = @Column(name="previousReport_personalContribution"))
    } )
    private ReportCostValues previousReport;




} 

希望这有助于!!!!!