I'd like to point out that not every DB supports creating sequences...
If you want to use a DB generated key, the choice is BMP, which will tie
your implementation to a specific DB.
Also, the primaryKey not necesarily needs to be ordered; that is, if you
want to know
the order in which records are created, put a timestamp field(CREATION) to
know when was it
created; this kind of problem may arise with 'regular' DB programming
HTH
JP
BTW, look in the archives, and you'll find discussions about generating UIDs
for use as PK in DB's.
You may find it useful
Rifle
-----Original Message-----
From: Stefan Hanenberg [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, July 05, 2000 11:06 AM
To: [EMAIL PROTECTED]
Subject: Re: DB Generated Primary Key
Hi Thomas,
I think there is NO possibility in EJB to use a DB generated primary
key! The code you are using has one fault:
If two enterprise objects will be created the same time, you cannot be
sure, that you got the "right newFoodID" for every object.
For example: you create object A and Object B the same time. When
ejbCreate for A is invoked getNewFoodId() returns id 1, When ejbCreate
for B is invoked getNewFoodId() returns id 2. But you cannot be shure
that A will be really created before B!!!
One solution could be, that getNewFoodId() created a new row in your
table, and the method ejbCreate does not use an INSERT but an UPDATE for
accessing the db. But in this case you have to be sure, that every
ejbContainer, where you bean is installed, allows you to create the
SQL-statements on you own. That is not required by the EJB-spec.
Within the ejb-spec (9.4.2) you can find:
"The container may create the representation of the entity in the
database immediately after ejbCreate(...) returns, or
it can defer it to a later time (for example to the time after the
matching ejbPostCreate(...) has been called, or to the end of the
transaction)."
That means: If your Bean should be deployed in every ejbContainer, you
never know when is accesses the database.
Stefan
Thomas Preston schrieb:
>
> 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".
===========================================================================
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".