Java – foreign key mapping in the embeddable class
I'm using eclipse link for JPA I have an entity that has a compound key consisting of two fields The following are the fields (members) of my embeddable primary key class
@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 will keep leave data related to staff, so I try to combine employee object and departure date to generate composite key Except for my logic, I am not allowed to have a foreign key mapping in the embeddable class When I try to use the JPA tool – > to generate a table from an entity, it gives the following error, which explains, but I don't get 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 that mean I can't have a key (compound key), it's also a foreign key Is there any way to complete this enterprise risk management? Please help. thank you
Solution
Do not put the relationship into the ID class, which is neither @ IdClass nor @ embeddedid@ An embeddable class can only contain @ basic, @ column, @ temporary, @ enumerated, @ lob, or @ embedded comments Everything is provider specific syntax (for example, hibernate allows this, but since you are using JPA RI's eclipse link, I suspect this is what you want)
The following is an example of 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
