• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

primary key generation

 
Jef Kelly
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I know this isn't on the exam, but I want to practice coding a few beans before I take the test, and I ran into a bit of a mire right out of the gate.

In the real world, is there an accepted or standard way of generating primary keys for entity beans?

Assuming a numeric PK...I've considered having a single go-to bean that reads the max value of the PK field in the database and then increments and doles them out. But if I do this as a bean, then every create() called on that bean's home is going to produce a bean that generates duplicates of every other bean in the pool! (more or less)

Static non-final field/method would seem to be an obvious bet, but they're disallowed.

Helper class? Vendor-specific dealie that I don't know about?

This seems like such a basic issue, but I haven't found any posts about it here....
 
Jason Hunt
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a db table to hold the key for each bean and create a entity bean mapping to this table, every time you get the pk, it increase the value.
 
Olexiy Prokhorenko
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you explain a bit more in details?
may be some example, if possible?

i am not sure that i understood. :-(
 
Jef Kelly
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jason. I must admit it took me a few seconds before I slapped my forehead and said "Of course!"

I set up a PRIMARY_KEY table that looks (currently) like this:

PK_PK, EMPLOYEE_PK
1 207

I'm now creating an entity with only one business method, getEmployeePK(). It returns the value of EMPLOYEE_PK and then increments EMPLOYEE_PK. The bean has create methods, and ejbRemove calls setRollbackOnly to keep anyone from removing this one and only row. It seems like a good idea to make the trans-attribute mandatory to make sure that this whole process is part of the calling transaction.

Sound about right?

Is this sort of approach..."industry standard?"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic