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:
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;
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?
JPA provides the @Embeddable annotation to declare that a class will be embedded by other entities.
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.
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.
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 !!!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With