JPA: compound primary key with @ManyToOne
I have to map a legacy database to JPA enity beans.
The legacy database has to following tables:
BaseComponent:
integer basecomp_id (PK)
... more fields
BasePac:
integer basepac_id (PK)
... more fields
Component:
integer basecomp_id (FK and 1. part of PK)
integer basepac_id (FK and 2. part of PK
... more fields
Thus, I need a compount key of one sort (@IdClass+@Id or @EmbeddedId) AND at the same time have the participating fields represent a @ManyToOne association each.
This does NOT work:
@javax.persistence.IdClass(ComponentPK.class)
publicclass Component
implements java.io.Serializable
{
@javax.persistence.Id
@javax.persistence.JoinColumn(name="basepac_id", columnDefinition="integer")
@javax.persistence.ManyToOne(fetch=EAGER)
private BasePac basePac;
public BasePac getBasePac(){return basePac;}
publicvoid setBasePac( BasePac basePac ){ this.basePac = basePac;}
@javax.persistence.Id
@javax.persistence.JoinColumn(name="basecomp_id", columnDefinition="integer")
@javax.persistence.ManyToOne(fetch=EAGER)
private BaseComponent baseComponent;
public BaseComponent getBaseComponent(){return baseComponent;}
publicvoid setBaseComponent( BaseComponent baseComponent ){ this.baseComponent = baseComponent;}
...
}
it results in an error:
org.hibernate.AnnotationException: Unable to find properties (basePacId, baseComponentId) in entity annotated with @IdClass:....Component
of course there are no fieds ...Id, because I am not interested in the int but in the associated object.
Any help is appreciated. Thanks!

