java – JPA具有前缀的多个嵌入字段?

使用JPA时,我想重复使用相同的嵌入式对象,如下所示:

@Entity
public class User {
    @Embedded
    public Address homeAddress;

    @Embedded
    public Address workAddress;
}

@Embeddable
public class Address {
    public String code;
    public String city;
    ...
} 

我可以使用@AttributeOverrides,@AttributeOverride和@Column来指定SQL列名称,但是它是动词.是否可以指定一个前缀添加到homeAddress和workAddress的每一列?

谢谢,

泽维尔

如果你想使用多个相同的Embedded类.您必须为所有列执行@AttributeOverrides.
尝试如下

参考JPA AttributeOverrides

@Embeddable
public class Address {
    private String state;
    @Column(name = "zip_code")
    private String zip;
}

@Entity(name = "Employee")
public class Employee implements Serializable {
    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "state", column = @Column(name = "province_1")),                       
        @AttributeOverride(name = "zip", column = @Column(name = "postal_code_2"))
    })
    private Address address_1;  

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "state", column = @Column(name = "province_2")),                       
        @AttributeOverride(name = "zip", column = @Column(name = "postal_code_2"))
    })
    private Address address_2;  

}   

我的建议,如果您的实体中有一个或多个嵌入式值.尝试使用@CollectionTable.

@CollectionTable(name = "EMPLOYEE_ADDRESS", joinColumns = @JoinColumn(name = "ADDRESS_ID"))
private List<Address> addressList;

参考JPA CollectionTable

http://stackoverflow.com/questions/12912063/jpa-multiple-embedded-fields-with-prefix

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:java – JPA具有前缀的多个嵌入字段?