• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Composite Primary Key

 
mini mehta
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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>?


Mini
 
Sree Nivas
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sujatha

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

Thanks
Vipin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic