java JPA 带有前缀的多个嵌入式字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12912063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA Multiple Embedded fields with prefix?
提问by xnopre
With JPA annoations, I want to reuse same embedded object like this :
使用 JPA 注释,我想重用相同的嵌入对象,如下所示:
@Entity
public class User {
@Embedded
public Address homeAddress;
@Embedded
public Address workAddress;
}
@Embeddable
public class Address {
public String code;
public String city;
...
}
I can specify SQL column names with @AttributeOverrides, @AttributeOverride and @Column, but it's verbos. Is it possible to specify just a prefix to add to each column for homeAddress and workAddress ?
我可以用@AttributeOverrides、@AttributeOverride 和@Column 指定SQL 列名,但它是冗长的。是否可以只指定一个前缀添加到 homeAddress 和 workAddress 的每一列?
Thanks,
谢谢,
Xavier
泽维尔
回答by Zaw Than oo
If you would like to use multiple same Embedded
class. You have to do @AttributeOverrides
for all columns.
Try as below;
如果您想使用多个相同的Embedded
类。你必须@AttributeOverrides
为所有列做。尝试如下;
Reference 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;
}
My suggestion, if there are one or more Embedded
value in your Entity
. Try to use @CollectionTable
.
我的建议是,如果Embedded
您的Entity
. 尝试使用@CollectionTable
.
@CollectionTable(name = "EMPLOYEE_ADDRESS", joinColumns = @JoinColumn(name = "ADDRESS_ID"))
private List<Address> addressList;
Reference JPA CollectionTable
参考JPA 集合表
回答by Vuk Djapic
Have the same problem, here is the solution. Works perfect, prefixes field names with the name of embedded property (in your case homeAddress_ and workAddress_)
有同样的问题,这里是解决方案。完美工作,使用嵌入属性的名称作为字段名称的前缀(在您的情况下是 homeAddress_ 和 workAddress_)
回答by Mahes
adding this works for me (i'm using hibernate as JPA provider though)
添加这对我有用(尽管我使用 hibernate 作为 JPA 提供程序)
<property name="hibernate.implicit_naming_strategy" value="org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl" />