If you're required to use triggers (or some other database-specific
mechanism) to generate primary keys, someone posted a clever solution to
this list a long time back:  use a finder method.  Since you control the
SQL issued for a finder method, just add a
Thingie findNew(Type param, Type param);
method and have it create the appropriate row in the SQL before
selecting it out.
BTW, the counter.jar is *not* portable.  It relies on pessimistic
concurrency behavior, and as far as I know the only other appserver
which supports this model is WebLogic when not clustered.  Even in WL 6,
the default behavior is now optimistic concurrency.
Here's a direct link to Brett's article about sequences (the original
link has moved on to other topics):
This is, I believe, the *only* portable way of generating sequence-type
keys in EJBland.  It astounds me that this is the case given that a)
nearly every database offers some sort of facility for generating keys
and b) the EJB container could fake it for the ones that don't.
Considering how common the need to generate primary keys is, I consider
this an grossly unacceptable deficiency in the spec.  I'm not normally
prone to believing conspiracy theories, but I have to wonder if the big
RDBMS vendors sitting on the EJB committee (read: Oracle and IBM) are
deliberately trying to keep CMP from becoming too popular.  This is just
too wierd.
Jeff Schnitzer

-----Original Message-----
From: elephantwalker [mailto:[EMAIL PROTECTED]]
Sent: Monday, May 14, 2001 8:38 AM
To: Orion-Interest
Subject: RE: Generating primary keys

two ways of doing this...
1. map your ejb pk to another ejb. The counter.jar does this. It creates
a long for every "beantype". See the faq at www.orionserver.com
<http://www.orionserver.com> . I think there is something under
<http://www.orionsupport.com> , also. In your ejbCreate(), get a
reference to the counter.jar, and then ask for the new pk :
  long id =
2. See the chain of articles by Brett McGlaughlin at
543-153#brett>   <http://www.flashline.com> . Instead of using an ejb to
generate your pk references, use jdbc and a stateless session bean. In
the end you do the same thing in your ejbCreate():
 long id = slsbRemote.getNextID("mybeanname");
And whatever you do, stay away from triggers. 
the elephantwalker

-----Original Message-----
[mailto:[EMAIL PROTECTED]]On Behalf Of Sergei Batiuk
Sent: Monday, May 14, 2001 10:08 AM
To: Orion-Interest
Subject: Generating primary keys

Hi mailing list,
I have the following problem: the primary key for my EJB is generated at
the database level ( a trigger ), so I don't want to generate it in the
ejbCreate() method. However, EJB specification says, that the use of the
primary key is a MUST. Does anyone have the solution for this problem?
Any help is greatly appreciated.

Reply via email to