许多嵌入式属性引用相同的Embeddable类

时间:2013-12-13 08:58:19

标签: java hibernate jpa persistence jpa-2.0

我想创建一个具有两个电话号码的类Exporter:固定电话和移动电话。 知道电话号码是由国家代码和号码组成的,我创建了一个Embeddable类:

@Embeddable
public class EmbeddablePhone implements Serializable {

    private static final long serialVersionUID = -6573789138766984739L;
    @Column(name = "COUNTRY_COD")
    private String countryCode;
    @Column(name = "PHONE_NUMBER")
    private String number;

    public EmbeddablePhone () {
    }

    //getters and setters 
}

如何在类Exporter中使用此类来引用固定电话和移动电话:

public class Exporter implements Serializable{

.....
......

    @Embedded
    private EmbeddablePhone  landLine;
    @Embedded
    private EmbeddablePhone  moblePhone;
    public Exporter () {

    }

    //getters and setters

    }

1 个答案:

答案 0 :(得分:3)

使用@AttributeOverride注释。

@Embedded
@AttributeOverrides({
  @AttributeOverride(name="countryCode", column=@Column(name="MOB_COUNTRY_COD")),
  @AttributeOverride(name="number", column=@Column(name="MOB_NUMBER"))})
private EmbeddablePhone moblePhone;