users@glassfish.java.net

TopLink JPA: Entities as Identity Fields

From: <glassfish_at_javadesktop.org>
Date: Thu, 10 May 2007 01:39:37 PDT

Hi,

is it possible to use Entities as Identity Fields or even in Composite Ids?

I have an entity which has a ManyToOne associations to two other entities. These should be also the composite id.

I read that the JPA Specification limits primary keys to simple types, but some implementations support it anyway.
http://incubator.apache.org/openjpa/docs/latest/manual/manual.html#ref_guide_pc_oid_entitypk

The following 'construction' throws an exception:
[i]Exception [TOPLINK-7150] (Oracle TopLink Essentials - 2006.8 (Build 060829)): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [model.StationPK] and those of the entity bean class [model.Station] must correspond and their types must be the same. Also, ensure that you have specified id elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class.[/i]


[pre]
@Entity
@IdClass(StationPK.class)
public class Station{
EntA a;
EntB b;

@Id
@ManyToOne(...)
@JoinColumn(...)
public EntA getA(){ ... }

@Id
@ManyToOne(...)
@JoinColumn(...)
public EntB getB(){ ... }
}

public class StationPK{
Integer a;
Integer b;

public boolean equals(...){ ... }

public int hashCode(){ ... }
}

@Entity
public class EntA{
@Id
Integer id;
}

@Entity
public class EntB{
@Id
Integer id;
}
[/pre]
[Message sent by forum member 'tgau' (tgau)]

http://forums.java.net/jive/thread.jspa?messageID=216276