2012-04-09 32 views
11

我对JPA使用eclipselink。我有一个实体,其中有一个复合键由两个字段组成。以下是我的嵌入式主键类的字段(成员)。嵌入类中的外键映射

@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工具- >从实体生成表时,它给出了如下的错误,这可以解释,但我没有得到它。

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. 

这是不是说,我不能有一个键(从组合键),这也是一个外键。有没有其他方法可以完成这种企业风险管理?请帮忙。谢谢

回答

12

请勿将关系放入ID类中,@IdClass@EmbeddedId之一。 @Embeddable类别可能只包括注释@Basic,@Column, @Temporal, @Enumerated, @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; 

    ... 
} 

HTH