• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get auto generated primary key from a CMP?

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are letting our database auto generate the primary key. I set the value to 0. The database ignores that and sets it to the next available number.

The problem is when I turn around and try to retrieve the primary key. I keep getting 0 back.



or this (really the same thing...)



At this point, primaryKey is still 0.

How do I get the new primary key?
 
Rusty Enisin
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the bean does not update the store until after I have finished with the bean. Or more likely, it updates the store whenever it feels like it, which may or may not be immediatly. Considering it is managing concurrant access and all that, it has the right to wait for others to finish. I cannot expect it to give me that value right away.

Also, the bean might have the 0 in memory not expecting that the database pulled a fast one on it and changed the value.... This I do not know...

Since there appears to be no way to get the pk value immediatly after creating I have tried to get creative. So here are some ideas that might work.

1. Temporarily set some unique value (timestamp to the second) in one of the other fields. This would work until you need to set that field too when you create. I do have some fields that I know will never get populated when I create. But what about all those other chaps with other apps around the world? They are up a creek....

2. Compare a list of available pks for the user from before and after create. This could have concurrency problems if the user does something silly, like, have two browsers open.

3. Make the user click a "new" button in the web app. Then they get a list of items with the new one (when selecting the bean will show up through a relationship). They then have to FIND the new one in the list and click on it. This would be really dumb. But it could be done.

This list could go on. But I am really stretching things here. Is there a better way to do this? Something more realistic? Some mothod like commitAlreadyAndGiveMeTheFreakinPk()?

me.bowToTheGuruWhoCanAnswerThis(guruWithAnswer)...
 
Rusty Enisin
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. So I am talking to myself today. I implemented idea 1 above. It works. I had to create a new finder method so I could find it. It is not the most optomized way I would expect to do this. But, it works. Because the timestamp is to the millisecond and the container is watching concurrent access, concurrent access problems are blown out of the arena. The database record is guranteed to be unique (my own temporary private primary key).

Then once I get the new bean, I clear the temp timestamp field I set, get the pk and all is happy.

I am really hoping that someone will read this and then tell me how I should really do it.

Ciao
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic