我正在使用
eclipselink进行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 }
我的实体将要保留与工作人员有关的休假数据,所以我试图结合员工对象和离开日期来生成复合密钥.除了我的逻辑,不允许我在embeddable类中有一个外键映射.当我尝试使用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.
这是不是意味着我不能有一个键(复合键),它也是一个外键.有没有办法完成这个企业风险管理?请帮忙.谢谢
解决方法
不要将关系放入ID类,既不是@IdClass或@EmbeddedId. @Embeddable类只能包含@Basic,@Column,@Temporal,@Enumerated,@Lob或@Embedded的注释.一切都是提供者特定的语法(例如,Hibernate允许这样做,但是由于您使用的是JPA RI的EclipseLink,我怀疑这是您想要的).
以下是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