I think that you are confused about how the seq gets used in code snippet.
It is called by the method (select name.nextval ....), a container managed
variable is set from the return value, and then the seq is never used again.
  If you get back 1, I don't care when the container does the insert, the pk
*will be* 1 (the container will just persist the value that I set in
ejbCreate).  The container can insert the row with pk 2 before inserting row
with pk 1.  That is ok.


>From: Stefan Hanenberg <[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 16:48:45 +0200
>
> > Your argument implies that it matters which row gets pk 1 and which gets
>pk
> > 2 when two ejb entity objects are created at the same time, but to me it
> > doesn't.  All I care about is that each of the objects gets a distinct
>pk
> > and a known pk.  When the getNewFoodId() method returns I will *always*
>get
> > a unique known pk.  So what is the problem?  I don't care which one gets
>1
> > and which one gets 2 as long as I know which gets which (and i do know
> > because of the return value).
>
>But have a look at the following situation:
>
>The entity Objects a + b should be created. getNewFoodId() for A returns
>1 and getNewFoodId() for B returns 2. So within A you set the
>pk-attribute id to 1 and within B id is 2.
>After that the container creates the representations of b and a (first b
>and then a....you cannot be sure what object he creates first!!!). Then
>the database-entry 1 contains the attributes of B, and conversely. But
>when the client asks for the attribute id of B he gets 1!
>So if he creates a primary key with the 1 and executes the
>findByPrimaryKey() of the client gets the object A!!!!!
>
>I think concerning unknown primary keys and database generated pks there
>is a lack in the ejb-spec. Most of the discussion was about to create a
>unique identified in your own. If the algorithm creates really unique
>keys.....that would be perfect.
> >
> > I am interested to continue this discussion because the db gen'd pk
>thread
> > comes up often with no "right way" to do it closure that I have seen.
> >
> > >From: Stefan Hanenberg <[EMAIL PROTECTED]>
> > >To: A mailing list for Enterprise JavaBeans development
> > ><[EMAIL PROTECTED]>,        [EMAIL PROTECTED]
> > >Subject: Re: DB Generated Primary Key
> > >Date: Wed, 05 Jul 2000 16:06:18 +0200
> > >
> > >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".
> >
> > ________________________________________________________________________
> > Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
>
>--
>***************************************************************
>Dipl.-Wirt.Inform. Stefan Hanenberg
>Mathematics & Computer Science
>University of Essen
>Sch�tzenbahn 70
>D-45117 Essen
>Germany
>
>email: mailto:[EMAIL PROTECTED]
>www: http://www.cs.uni-essen.de/shanenbe/
>phone: ++49-201-183-2168
>fax: (+49) 201-183 2419
>***************************************************************
>
>===========================================================================
>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