Hi Richard,
according to the spec (sec.10.8.3)
you can write a CMP ejb with an unknown primary key class as long as
you specify in the deployment descriptor the primary key class as type
java.lang.Object.
This means, of course, that you do not specify any <primkey-field> in the
deployment
descriptor as these are not known to the Bean Provider and therefore needs
to be set
at Deployment time. If you declare cmp-fields, you do not need to declare
the primary key
as this will determined at deployment time and the container will add the
extra fields to the
concrete implementation.
So back to your example, I would write it like this:
public abstract class CustomerBean implement EntityBean {
//public Integer ejbCreate( ){}
public Object ebjCreate() throws CreateException {}
//the rest as before
}
public interface CustomerHome extends EJBLocalHome{
public Customer create() throws CreateException;
// public Customer findByPrimaryKey(Integer primKey)
public Customer findByPrimaryKey(Object 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> -->
<prim-key-class>java.lang.Object</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>
Best regards,
Max
----- Original Message -----
From: "Richard Monson-Haefel" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, June 16, 2001 5:52 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".