Embeddable类中的外键映射 [英] Foreign key mapping inside Embeddable class

查看:190
本文介绍了Embeddable类中的外键映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 eclipselink 获取 JPA 。我有一个实体,它有两个字段组成的复合键。以下是我的可嵌入主键类'字段(成员)。

I am using eclipselink for JPA. I have an entity, which has a composite key fabricated out of two fields. Following is my Embeddable primary key class' fields(members).

    @Embeddable
    public class LeavePK {
       @ManyToOne(optional = false)
       @JoinColumn(name = "staffId", nullable = false)
       private Staff staff;
       @Temporal(TemporalType.TIMESTAMP)
       private Calendar date;
       //setters and getters
    }

我的实体将暂缓离开与员工相关的数据,所以我试图结合员工对象和离开日期来生成复合键。除了我的逻辑,它不允许我在嵌入类中有一个外键映射。当我尝试使用 JPA工具 - >从实体生成表时,它会给出如下错误,这解释了,但我没有得到它。

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.

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.

这是否意味着,我不能拥有一个也是外键的密钥(来自复合密钥)。有没有其他方法可以完成此ERM?请帮忙。谢谢

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

推荐答案

不要将关系放入ID类,也不要将 @IdClass @EmbeddedId @Embeddable 类可能只包含注释 @Basic @Column @Temporal @Enumerated @Lob ,或 @Embedded 。其他一切都是特定于提供者的语法(例如Hibernate允许这样做,但是因为你正在使用EclipseLink,这是JPA RI,我怀疑这是你想要的)。

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).

以下是JPA PK / FK映射示例:

Here's an example JPA PK/FK mapping:

@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

这篇关于Embeddable类中的外键映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆