Nested embeddable - AttributeOverride for embeddable within embeddable

北城余情 提交于 2019-11-30 01:18:08

问题


I have class Money which is an @Embeddable

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

When I useit multiple time inside entity, everything works fine. For example

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

Code above works perfectly.

Now the problem occurs when I have another @Embeddable that I want to have Money instances in it and that @Embeddable is used by an entity multiple times. Example:

  1. Embeddable

    @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

     @Entity
     public class ReportCostEntity implements Identifiable<Long>, Serializable {
    
        @Id
        private Long id;
    
        @Embedded       
        private ReportCostValues contracted;
    
        @Embedded       
        private ReportCostValues current;
    
        @Embedded        
        private ReportCostValues previousReport;
    

This code above will not work. Any ideas how to approach this problem?


回答1:


Hi you have to use @AttributeOverrides judicially,you have to override attributes once again in entity that you have done in embeddable ReportCostValues class, hope code below is what you are looking for.

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




} 

Hope this helps !!!!!



来源:https://stackoverflow.com/questions/9845575/nested-embeddable-attributeoverride-for-embeddable-within-embeddable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!