Java 可嵌入类内的外键映射

Java 可嵌入类内的外键映射,java,jpa,entity-relationship,composite-key,embeddable,Java,Jpa,Entity Relationship,Composite Key,Embeddable,我正在为JPA使用eclipselink。我有一个实体,它有一个由两个字段组成的复合键。以下是我的可嵌入的主键类”字段(成员) 我的实体将保存与员工相关的休假数据,因此我尝试将员工对象和休假日期组合起来以生成复合密钥。除了我的逻辑之外,它不允许我在可嵌入类中有外键映射。当我尝试使用JPA工具-->从实体生成表时,它给出了如下错误,这可以解释,但我没有得到它 org.eclipse.persistence.exceptions.ValidationException Exception Descr

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

我的实体将保存与员工相关的休假数据,因此我尝试将员工对象和休假日期组合起来以生成复合密钥。除了我的逻辑之外,它不允许我在可嵌入类中有外键映射。当我尝试使用JPA工具-->从实体生成表时,它给出了如下错误,这可以解释,但我没有得到它

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?请帮忙。谢谢

不要将关系放入ID类,无论是
@IdClass
还是
@EmbeddedId
类。
@Embedded
类只能包括注释
@Basic
@列
@时态
@枚举
@Lob
@Embedded
。其他一切都是特定于提供者的语法(例如Hibernate允许这样做,但由于您使用的是EclipseLink,即JPA RI,我怀疑这是您想要的)

以下是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;

    ...
}

@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;

    ...
}