I have an entity with composite key so I use the @Embeddable and @EmbeddedId annotations. Embeddable class looks like this :
@Embeddable
public class DitaAdm
Thought I would post the solution that I found which is compliant with the JPA 2.0 specification and appears to function the same way.
First off, the JPA 2.0 specification can be found here: JSR-000317 Persistence Specification for Eval 2.0 Eval. The relevant section would be 2.4.1 "Primary Keys Corresponding to Derived Identities"
Here is an example using the classes you specified:
Embedded Id Class:
@Embeddable
public class DitaAdminAccountSkillPK implements Serializable {
//No further annotations are needed for the properties in the embedded Id.
//Needs to match the type of the id of your DitaAdmin object. I added 'Id' to the end of the property name to be more explicit.
//Making the assumption here that DitaAdmin has a simple Integer primary key.
private Integer adminId;
//Needs to match the type of the id of your DitaAccount object. I added 'Id' to the end of the property name to be more explicit.
//Making the assumption here that DitaAccount has a simple Integer primary key.
private Integer accountId;
//I'm adding a third property to the primary key as an example
private String accountName;
//constructor, getters, setters...
//hashCode() and equals() overrides
}
"Dependent" Entity Class:
@Entity
public class DitaAdminAccountSkill {
@EmbeddedId
//Any overrides to simple Id properties should be handled with an attribute override
@AttributeOverride(name = "accountName", column = @Column(name = "account_name"))
private DitaAdminAccountSkillPK id;
//MapsId refers to the name of the property in the embedded Id
@MapsId("adminId")
@JoinColumn(name="admin_id")
@ManyToOne
private DitaAdmin admin;
@MapsId("accountId")
@JoinColumn(name="account_id")
@ManyToOne
private DitaAccount account;
//constructor, getters, setters...
}
"Parent" Entity class:
public class DitaAdmin {
@Id
private Integer id;
//...
//Now your mappedBy attribute can refer to the admin object defined on DitaAdminAccountSkill which is also part of the Primary Key
@OneToMany(fetch = FetchType.LAZY, mappedBy="admin")
private List accountSkills;
//...
}