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