Compound primary keys are used for beans with two or more fields as the primary key. A compound primary key is implemented by creating a special key class containing the fields.
Map relations are allowed for entity beans with a primary key pair where one of the primary keys is an identifying relation. The child bean's identifying relation implicitly selects the parent bean and the other key is used as the Map relation's key field.
An identifying relation with a two-field compound primary key
can support a map relation. In this example, each
he takes. The
has two primary keys: the
The two keys are identifying relations since they refer directly
indirectly through their keys.
Part of the the primary key class looks like:
map relation belongs to the
bean. The map's key is the
object (not the key for
.) Resin-CMP will use the
as the field of the composite key, and it will
argument to the
the other key. Since a
uniquely identify a
, Resin-CMP can use the combined
key to look up the
The map relation is read-only since its data comes from the
table. In other words, it makes no sense to call
beans are created with
, it doesn't make sense for the
to be writable.
is a live object. In other words, when a new
created or an old one is destroyed, the
object is automatically
updated. Of course, the
needs to be used in the same transaction
The database has tables for
table is dependent on
Because of this dependency, Resin-CMP will automatically delete a student's
grades when the student is removed.
's map relation is implemented with one main SELECT query
method is implemented by calling
returns a set of
beans and the query looks like:
method takes a
and returns a
object. The query looks like:
The example client servlet uses the
like any other
. The servlet iterates over all the students
and displays their grades. The
are found using
iterator and then the
itself is found using the
A compound primary key is made up of a combination of
persistent fields or identifying relations. This example uses two
Only the home interface
is different for a compound primary key bean. The local interface and
the bean implementation are just like single key beans.
Compound primary keys may have any number of fields. The special
case of two fields with at least on being an identifying relation
collections in the parent map, like the
method in the
The identifying key corresponding to the bean, e.g.
is used to select the grades. The second key, in this case
, is used as the
. The bean itself is
The bean key is a public class with public fields for each
key component. In this case, the
both a public field for the student and the
The compound key class must override the
methods. If all the component keys are identical, the
keys must be identical, even if they are separate Java objects.
Resin-CMP needs both
the bean's caching.
Many database entities need to store named attributes, e.g. a
needs to store her
, indexed by the
she took. By
implementing this common pattern directly with the standard
and avoiding working directly with JDBC, applications using Resin-CMP
can create cleaner Java code, avoid common maintenance issues
during development, and take automatic advantage of database caching.