File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Composite Primary Key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Composite Primary Key" Watch "Composite Primary Key" New topic

Composite Primary Key

mini mehta
Ranch Hand

Joined: Oct 22, 2000
Posts: 120

Just a doubt about the Composite Primary Key. Sure one has to create a Primary key class and put that in <prim-key-class> tag. But what about <primkey-field>? Do we need to have multiple <primkey-field> for each <cmp-field> which is part of the Primary Key?

Also do we need to make sure that the Primary key class instance variables are named as they appear in <cmp-field>?

Sree Nivas
Ranch Hand

Joined: Jul 24, 2003
Posts: 95
Again as per the spec:

The prim-key-class element contains the fully-qualified name of an entity bean's primary key class.

The primkey-field element is used to specify the name of the primary key field for an entity with container-managed persistence.

The primkey-field must be one of the fields declared in the cmp-field element, and the type of field must be the same as the primary key type.

The primkey-field element is not used if the primary key maps to multiple container-managed fields (i.e., the key is a compound key). In this case, the fields of the primary key class must be public, and their names must correspond to the field names of the entity bean class that comprise the key.

So we need not have multiple <primkey-field> for each <cmp-field> which is part of the Primary Key. Hence ur second question not applies.

The following is the DTD entry of DD corresponding to entity element:

<!ELEMENT entity(description?, display-name?, small-icon?, large-icon?, ejb-name, home, remote, ejb-class, persistence-type, prim-key-class, reentrant, cmp-filed*, primkey-field?, env-entry*, ejb-ref*, security-role-ref*, resource-ref*)>

As per the above DTD entry prim-key-class is mandatory for entity beans and primkey-field is optional.
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
To put it in another way

<primkey-field> is used when there is only one primary key field and
<prim-key-class> is used in case of a composite primarkey key.

At a time either only one of them can be used.

In case of composite primary key, the bean provider creates a primary key class and specifies the class name in <prim-key-class> tag.

Hope it helps

Vipin Mohan
Ranch Hand

Joined: Nov 15, 2003
Posts: 79
hi Sujatha

I think <prim-key-class> element is always required whether its a single field or a composite field primary key.

I agree. Here's the link:
subject: Composite Primary Key
jQuery in Action, 3rd edition