问题
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
}
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.
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
回答1:
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).
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
来源:https://stackoverflow.com/questions/10078224/foreign-key-mapping-inside-embeddable-class