In ejbCreate, call a method which gets the next primary key from the db.
Here is an example of ejbCreate and methhod that gets next seq from db (this
can be a CMP bean):

        public void ejbCreate( String Product_code) throws RemoteException,
CreateException
        {
                try {
                        //create primary key
                        m_Food_id = getNewFoodId();

                        //init rest of fields
                        //fields passed in to constructor
                        m_Product_code = Product_code;

                        //fields not passed in to constuctor
                        m_Ingredient = null;

                }
                catch(Exception exp) {
                        throw new CreateException( exp.getMessage() );
                }
        }

        private Integer getNewFoodId() throws Exception
        {
                Connection connect   = null;
                Statement  statement = null;
                ResultSet  results   = null;
                Integer FoodId = null;

                try {
                        connect   = getConnection();
                        statement = connect.createStatement();
                        statement.executeQuery("select " + getSequenceName() + 
".nextval from
DUAL");
                        results = statement.getResultSet();
                        if ((results != null) && (results.next()))
                        {
                                FoodId   = new Integer( results.getInt(1) );
                        }
                        else {
                                throw new CreateException ("ejbCreate: sequence failed 
to return a
value");
                        }
                }
                catch (SQLException sqe) {
                        throw new CreateException (sqe.getMessage());
                }
                finally {
                        if (statement != null) statement.close();
                        if (results   != null) results.close();
                        connect.close();
                }
                return FoodId;
        }

>From: "Atul Kulkarni(CTS)" <[EMAIL PROTECTED]>
>Reply-To: A mailing list for Enterprise JavaBeans development
><[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Re: DB Generated Primary Key
>Date: Wed, 5 Jul 2000 11:53:23 +0530
>
>As I did not receive any post on this do I consider that there is no
>solution to this problem? Any hint will suffice.
>
>Regards,
>Atul.
>
>-----Original Message-----
>From: Atul Kulkarni(CTS) [mailto:[EMAIL PROTECTED]]
>Sent: Saturday, July 01, 2000 2:17 AM
>To: [EMAIL PROTECTED]
>Subject: DB Generated Primary Key
>
>
>Hi all:
><Q1>
>I have a case where I create many records of type PayObject where the
>primary ky is generated by the DB itself. If in this case I want to map an
>entity bean to this table, how do I do it?
>
>Also in the database there are some other records being inserted in the
>same
>transaction which hold a reference to PayObject object ( ie have a column
>which holds the primary Key of PayObject ). So how does the object know its
>own primary key on creation?
></Q1>
>
>I am using WL 4.5.1 with Toplink.
>
>Thanx in advance.
>Atul.
>
>===========================================================================
>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".
>

________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.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".

Reply via email to