wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes prim-key-class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "prim-key-class" Watch "prim-key-class" New topic
Author

prim-key-class

Sankar Subbiramaniam
Ranch Hand

Joined: Oct 03, 2000
Posts: 116
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

Joined: Nov 06, 2005
Posts: 44
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


Vikas Prasad<br />SCJP 1.4 SCWCD1.4 SCBCD1.3
Sankar Subbiramaniam
Ranch Hand

Joined: Oct 03, 2000
Posts: 116
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

Joined: Nov 06, 2005
Posts: 44
may be when container inplements the class it calls the non argument constructor of the class.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Sankar Subbiramaniam
Ranch Hand

Joined: Oct 03, 2000
Posts: 116
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

Joined: Sep 29, 2002
Posts: 1683
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

Joined: Oct 03, 2000
Posts: 116
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 ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: prim-key-class
 
Similar Threads
Jboss 3.0.4 CMP2 with Oracle 8.1.6
Need confirmation for single primary key class
EJB CMP Primary Key problem
2 primary keys??
CMP 2.0 with oracle-sequence and composite primary key