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!

[2538 byte] By [michaelhhha] at [2007-11-27 10:37:10]
# 1

Ok, I solved it myself. Sorry, no Duk Stars for anyone ;-)

The solution is to just do go ahead as usual. But repeat all the annotations but the @Id in the matching attributes of the pk class referred in @IdClass. Thus all these attributes are classes, none is an int (as the key parts are). The int is only used internally.

michaelhhha at 2007-7-28 18:46:19 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...