使用JPA annoations,我想重用相同的嵌入对象:
@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列名,但它是verbos。是否可以仅指定添加到homeAddress和workAddress的每列的前缀?
谢谢,
济
答案 0 :(得分:8)
如果您想使用多个相同的Embedded
课程。您必须为所有列执行@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;
}
我的建议是,Embedded
中有一个或多个Entity
值。尝试使用@CollectionTable
。
@CollectionTable(name = "EMPLOYEE_ADDRESS", joinColumns = @JoinColumn(name = "ADDRESS_ID"))
private List<Address> addressList;
答案 1 :(得分:3)
有同样的问题,这是解决方案。完美的工作,前缀字段名称与嵌入属性的名称(在您的情况下为homeAddress_和workAddress _)
答案 2 :(得分:0)
为我添加此功能(尽管我将hibernate用作JPA提供程序)
<property name="hibernate.implicit_naming_strategy" value="org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl" />