そごうソフトウェア研究所

SOA、開発プロセス、ITアーキテクチャなどについて書いています。Twitterやってます@rsogo

必要に迫られて勉強始めました

複合IDの場合どうすればいいのかな。

JSR-000220 Enterprise JavaBeans 3.0ejb-3_0-fr-spec-persistence.pdfを読むのがやっぱりよさそう。

「2.1.4 Primary Keys and Entity Identity」より抜粋。
Every entity must have a primary key.

A composite primary key must correspond to either a single persistent field or property or to a set of
such fields or properties as described below. A primary key class must be defined to represent a composite
primary key. Composite primary keys typically arise when mapping from legacy databases when the
database key is comprised of several columns. The EmbeddedId and IdClass annotations are used
to denote composite primary keys. See sections 9.1.14 and 9.1.15.


複合主キーは一つの永続フィールドまたはプロパティ、または以下に記述するようなフィールドまたは
プロパティのセットに一致しなければならない。
主キークラスは複合主キーを表すように定義しなくてはならない。
複合主キーはレガシーデータベースからのマッピングや、
データベースキーが複数カラムで構成されている場合に一般的に使用される。
複合主キーをあらわす為にEmbeddedIdとIdClassアノテーションが使われます。

・ A composite primary key must either be represented and mapped as an embeddable class (see
Section 9.1.14, "EmbeddedId Annotation") or must be represented and mapped to multiple
fields or properties of the entity class (see Section 9.1.15, "IdClass Annotation").

複合主キーはembeddableクラスとして表現、マッピングされるか、
Entityクラスの複数のフィールド、またはプロパティとして表現、マッピングされる必要があります。

・ If the composite primary key class is mapped to multiple fields or properties of the entity class,
the names of primary key fields or properties in the primary key class and those of the entity
class must correspond and their types must be the same.

複合主キークラスがエンティティクラスの複数のフィールド、プロパティとしてマッピングされる
のであれば、主キーのフィールドまたはプロパティの名前と型が一致している必要があります。


ふむふm。
EmbeddedIdとIdClassを使えってことね。

使い方は以下がサンプルつきでよく分かる。
http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html