22

我有类 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;
    

上面的这段代码不起作用。任何想法如何解决这个问题?

4

1 回答 1

38

嗨,您必须正确使用@AttributeOverrides,您必须在可嵌入类中完成的实体中再次覆盖属性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;




} 

希望这可以帮助 !!!!!

于 2012-03-24T17:41:59.507 回答