Michael Pikounis wrote:
> I am looking for a good way to generate primary keys for my entities and I
> was wondering if there is a "standard" way.
I'm sure many people will immediately tell you to use database sequence
tables, like Oracle's CREATE SEQUENCE construct.
Personally, I don't like this approach (which puts me in a very small
minority!). To me, the mechanics and logic used to generate an OID have
nothing to do with the persistence layer, and consequently I like
treating the generation of OIDs as a business logic function. See
http://www.ambysoft.com/mappingObjects.pdf for a similar view.
Additionally, if you have ordered integers as OIDs, then people WILL
begin writing database queries and such that rely upon that
ordering--whether they should or not. OIDs should be opaque and
meaningless--they should serve only to take up "space" in an address
space (in much the same way that bodies take up space in the physical
address space). They shouldn't tell you anything else.
In our project, we've written an OID generator that draws its initial
seed from a databse or a flat file. It then (very quickly) munches that
number and shuffles the bits around and ultimately results in creating a
long value without losing any of the information. It's basically like a
bad random number generator that makes sure never to output the same
value twice.
So, standard? No. Better than sequentially increasing integer values?
Yes.
Cheers,
Laird
===========================================================================
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".