• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CMP and primary key

 
Jayashree Mohan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had a question ....

With CMP, the primary key for an entity bean may be automatically generated by the container if the entity bean meets certain
requirements. Which of the following is true. Select 1 correct Choice.

A.
In the deployment descriptor, the primary key class must be defined as a java.lang.Object. The primary key field must not specified.

B.In the home interface, the argument of the findByPrimaryKey method must be a java.lang.Object.

C.In the entity bean class, the return type of the ejbCreate method must be a java.lang.Object.

D.All of the above are requirements.

E.None of the above are requirements.

I think the answer is E coz K&B states that it is the responsibility of the bean provider to write code for teh primary key generation and should be generated before the ejbcreate method completes. But the mock exam states that the answer is D..which is correct?
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the version of the model exam that you are using. D might be the right answer for some older versions of EJB like EJB 1.1 bcos in EJB 1.1, I heard that this kind of thing can happen, but in EJB 2.0, as you said it is the bean provider's responsibility to generate the PK in ejbCreate() or else the insert will not happen
 
Ramakrishnan Viswanathan
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer D is correct.

Please refer to the following section of EJB 2.0 Spec

10.8.3 Special case: Unknown primary key class
_________________________________________________

In special situations, the entity Bean Provider may choose not to specify the primary key class or the primary key fields for an entity bean with container-managed persistence. This case usually happens when the entity bean does not have a natural primary key, and/or the Bean Provider wants to allow the Deployer using the Container Provider�s tools to select the primary key fields at deployment time. The entity bean�s primary key type will usually be derived from the primary key type used by the underlying
database system that stores the entity objects. The primary key used by the database system may not be known to the Bean Provider.

In this special case, the type of the argument of the findByPrimaryKey method must be declared as java.lang.Object. The Bean Provider must specify the primary key class in the deployment descriptor as of the type java.lang.Object.

When defining the primary key for the enterprise bean, the Deployer using the Container Provider�s tools will typically add additional container-managed fields to the concrete subclass of the entity bean class (this typically happens for entity beans that do not have a natural primary key, and the primary keys are system-generated by the underlying database system that stores the entity objects). In this case, the Container must generate the primary key value when the entity bean instance is created (and before
ejbPostCreate is invoked on the instance.)
 
Greg Charles
Sheriff
Posts: 3002
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A. I would say this is sometimes, but not always, true. The primary key can be one of the the CMP fields. In this case, you can specify its type as String, Integer, etc., and you must specify the primary key field. On the other hand, you can also define a primary key class consisting of one or more of the CMP fields. In that case, you don't specify the primary key field(s) in the DD, only the primary key class.

B. True. The primary key can be a String or wrapper type, or it can by a user-defined primary key class, but it must be an Object. It can never be a primitive.

C. True. The return type of the ejbCreate method must be the primary key type. For CMPs, the bean provider returns null, but the return type is still the primary key type: String, Integer, custom PK class, etc.

I think I've got that all right. So, that makes D nearly right, but not quite. E is definitely wrong. What mock exam is this from?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic