我有班钱是@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被实体多次使用时,就会出现问题。例:
可嵌入
@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
public class ReportCostEntity implements Identifiable, Serializable {
@Id private Long id; @Embedded private ReportCostValues contracted; @Embedded private ReportCostValues current; @Embedded private ReportCostValues previousReport;
上面的这段代码不起作用。任何想法如何解决这个问题?
嗨,您必须@AttributeOverrides司法使用,您必须在可嵌入ReportCostValues类中完成的实体中再次覆盖属性,希望下面的代码是您想要的。
@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; }
希望这可以帮助 !!!!!