Hi Richard,
There are two ways in which a primary key can be specified at the deployment
time.
1. Deferred primary key.
2. Invisible primary key.
Deferred Key :
This is the case when deployer choses one or more cmp-fields as primary key
at the deployment time. The type of primary key is specified as
java.lang.Object in the deployment descriptor. If the number of selected
cmp-fields are more than one, then it is a case of deferred compound primary
key. Different vendor may provide different implementation strategy for
compound-keys. Note that primary keys at the object level do have a
corresponding field in the persistent store.
Invisible Key :
This is the case when primary key is not any of the cmp-field of the bean
class. Rather it is one or more fields of the persistent store. The type of
primary key, as in previos case, is specified as java.lang.Object in the
deployment descriptor. One of the implementation strategy suggested by the
spec is to allow deployer (or container ??) using container tools to add
additional fields in the bean class. Since these fields are generated at the
deployment time, they are not available to the bean provider. Note that in
this case also, primary keys at the object level do have a corresponding
field in the persistent store.
If the primary key field(s) are not persisted along with the other
cmp-fields, then the conatiner will not be able to uniquely identify a
bean (or a record in the persistennt store) for example in findByPrimaryKey,
or in ejbLoad and in other callbacks. This way it seems corresponding
persistent fields are must for primary key implementation in CMP beans.
Regards,
Hemant
www.pramati.com
----- Original Message -----
From: "Richard Monson-Haefel" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, June 16, 2001 10:22 PM
Subject: CMP 2.0: Auto-generated primary keys
With auto-generated primary keys the value of the primary key is
obtained from the container, which may generate the key itself or use
some kind of auto-generate facilities of the database, like
auto-increment.
If I want a CMP 2.0 entity bean, say the Customer EJB, to use a
auto-generated primary key of type Integer, then it appears that I would
have to declare the primary key as "undefined" or "deferred" at bean
development time. Is that correct?
Can I define the primary key type, but not corresponding persistent
fields? If you define abstract accessor methods for primary key's
field(s), then you must set the primary key field in the ejbCreate()
method (sec. 10.3.5), which I don't want to do with auto-generated
primary keys. I want the container to set auto-generated keys.
Is the following valid?
public abstract class CustomerBean implement EntityBean {
public Integer ejbCreate( ){}
public void ejbPostCreate(){}
public abstract String getLastName(){}
public abstract void setLastName(){}
public abstract String getFirstName(){}
public abstract void setFirstName(){}
// call back methods
}
public interface CustomerHome extends EJBLocalHome{
public Customer create() throws CreateException;
public Cusotmer findByPrimaryKey(Integer primKey)
throws FinderException;
}
// deployment descriptor
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>CustomerEJB</ejb-name>
<local-home>CustomerHome</home>
<local>Customer</remote>
<ejb-class>CustomerBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<cmp-field><field-name>lastName</field-name></cmp-field>
<cmp-field><field-name>firstName</field-name></cmp-field>
</entity>
Notice that a primkey-field is not specified and that neither of the
persistent fields match the prim-key-class type.
Richard
--
Richard Monson-Haefel
Author of Enterprise JavaBeans, 2nd Edition (O'Reilly 2000)
Co-Author of Java Message Service (O'Reilly 2000)
http://www.jMiddleware.com
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".