java Embeddable 类中的外键映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10078224/
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
Foreign key mapping inside Embeddable class
提问by Ahamed
I am using eclipselink
for JPA
. I have an entity, which has a composite keyfabricated out of two fields. Following is my Embeddableprimary key class' fields(members).
我正在使用eclipselink
for JPA
。我有一个实体,它有一个由两个字段组成的复合键。以下是我的Embeddable主键类的字段(成员)。
@Embeddable
public class LeavePK {
@ManyToOne(optional = false)
@JoinColumn(name = "staffId", nullable = false)
private Staff staff;
@Temporal(TemporalType.TIMESTAMP)
private Calendar date;
//setters and getters
}
My entity is going to hold leave data related to a staff, so I am trying to combine staff object and leave date to produce composite key. Apart from my logic, it is not allowing me to have a foreign key mapping inside embeddable class. When I try to use JPA tools--> Generate Tables From Entity, it gives error as below, which explains, but I am not getting it.
我的实体将保存与员工相关的休假数据,因此我尝试将员工对象和休假日期结合起来以生成复合键。除了我的逻辑之外,它不允许我在可嵌入类中有外键映射。当我尝试使用JPA tools--> Generate Tables From Entity 时,它给出了如下错误,这解释了,但我没有得到它。
org.eclipse.persistence.exceptions.ValidationException
Exception Description: The mapping [staff] from the embedded ID class [class rs.stapp.entity.LeavePK] is an invalid mapping for this class. An embeddable class that is used with an embedded ID specification (attribute [leavePK] from the source [class rs.stapp.entity.Leave]) can only contain basic mappings. Either remove the non basic mapping or change the embedded ID specification on the source to be embedded.
Does it mean, I cannot have a key(from composite key) which is also a foreign key. Is there a alternative way to accomplish this ERM? Please help. Thanks
这是否意味着,我不能有一个也是外键的键(来自复合键)。有没有其他方法来完成这个 ERM?请帮忙。谢谢
回答by Kawu
Don't put relationships into ID classes, neither for @IdClass
or @EmbeddedId
ones. An @Embeddable
class may only include the annotations @Basic
, @Column
, @Temporal
, @Enumerated
, @Lob
, or @Embedded
. Everything else is provider-specific syntax (e.g. Hibernate allows this, but since you're using EclipseLink, which is the JPA RI, I doubt this is what you want).
不要将关系放入 ID 类中,无论是 for@IdClass
还是@EmbeddedId
one。一@Embeddable
类可能只包括注释@Basic
,@Column
,@Temporal
,@Enumerated
,@Lob
,或@Embedded
。其他一切都是提供者特定的语法(例如,Hibernate 允许这样做,但由于您使用的是 EclipseLink,即 JPA RI,我怀疑这是否是您想要的)。
Here's an example JPA PK/FK mapping:
这是一个 JPA PK/FK 映射示例:
@Entity
@Table(name = "Zips")
public class Zip implements Serializable
{
@EmbeddedId
private ZipId embeddedId;
@ManyToOne
@JoinColumn(name = "country_code", referencedColumnName = "iso_code")
private Country country = null;
...
}
@Embeddable
public class ZipId implements Serializable
{
@Column(name = "country_code")
private String countryCode;
@Column(name = "code")
private String code;
...
}
HTH
HTH