Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

primary key generation

 
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....
 
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.
 
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?"
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic