Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested embeddable - AttributeOverride for embeddable within embeddable

Tags:

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?

like image 605
Pawel Szulc Avatar asked Mar 23 '12 19:03

Pawel Szulc


People also ask

What is@ embeddable annotation?

JPA provides the @Embeddable annotation to declare that a class will be embedded by other entities.

Can entity be embeddable?

An entity may have single-valued or collection-valued embeddable class attributes. Embeddable classes have the same rules as entity classes but are annotated with the javax. persistence. Embeddable annotation instead of @Entity.

What is embedded class in JPA?

JPA - Embeddable ClassesAn entity may have references of other non-entity classes. Such non-entity classes are called embeddable classes. All fields of an embeddable class are mapped to the owner entity table. @Embeddable annotation is used on the non-entity class.


1 Answers

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 !!!!!

like image 188
baba.kabira Avatar answered Sep 21 '22 11:09

baba.kabira