• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

prim-key-class

 
Sankar Subbiramaniam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) I have defined a cmp field of type in my CMP entity bean
2) The same field is also used as primary key.

Question:
Since primary key cannot be primitve, this filed has to be wrapped as Integer.
Is it sufficient that i just declare: <prim-key-class>java.lang.Integer</prim-key-class> ?

(or)

Should i, in addition convert the cmp field to be of type Integer ?

Thanks.

My guess: It is sufficient to declare <prim-key-class>java.lang.Integer</prim-key-class>. the container will take care of converting the int field to java.lang.Integer.
 
vikas prasad
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear sankar,
I think you are write.
Comtainer manages the bean persistence for both CMP and CMR fields you just have to declare the Abstract schema..
so the conversion is done automatically.
Cotainer figures it out by the abstract getters and setters in the bean class.
vikas prasad
SCJP1.4 SCWCD 1.4 SCBCD 1.3
 
Sankar Subbiramaniam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the requirements for a primary key class, it should have a default pconstructor with no arguments (spec 10.8.2, page 203).

But java.lang.Integer - does not meet this criteria.
 
vikas prasad
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be when container inplements the class it calls the non argument constructor of the class.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the requirements for a primary key class, it should have a default pconstructor with no arguments

This is only true where the PK maps to multiple fields in the entity bean class. For a PK that maps to a single field in the entity bean class, Integer is correct.
 
Sankar Subbiramaniam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quote from specs page 203, 10.8.1:

The Bean Provider uses the primkey-field element of the deployment descriptor to specify the container-managed field of the entity bean class that contains the primary key. The field�s type must be
the primary key type.


This means, if a cmp-field is also a primkey-field then it cannot be primitive type (because a primary key has to be an object). Is it correct ?

Googled and got the answer:
The cmp-field used as simple primary key can be primitive as long as it is possible for the container to convert it to its specified Wrapper type.

[ UD: removed link to copyrighted material ]

[ December 20, 2005: Message edited by: Sankar Subbiramaniam ]
[ August 03, 2007: Message edited by: Ulf Dittmer ]
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I won't believe everything you find in google ...

Any CMP field can be a PK so long as it is serializable. If you select a primitive as the PK and rely on the container doing a conversion to the wrapper, then this does not comply with the EJB spec (which is what matters to you).
 
Sankar Subbiramaniam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not understand your answer.
Lets take an example:
Entity baean has two fields:
int id
String name

Now in the DD, i specify as follows:
<prim-key-class>java.lang.Integer</prim-key-class>
<cmp-field> <field-name>id</field-name> </cmp-field>
<cmp-field> <field-name>name</field-name> </cmp-field>
<primkey-field>id</primkey-field>

Is this correct ? or should i change "id" type to java.lang.Integer ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic