| Autore |
Messaggio |
stefano.schivo
|
Thread
Postato:
31 gennaio 2005
Titolo:
auto-incrementing Primary keys?
|
|
|
Et voila: another entertaining doubt by Me! Today I would like to talk about Primary Keys.
I was wondering if there is a way to use auto-incrementing primary keys for our Entity Beans.. Could we do things in such way that we have not to tell the Bean its primary key when we create it (and so our admirable mySQL creates one on its own, thanks to auto-increment) and have it back in some ways? I tried to create a Bean (let's call it Fagiolo) using the usual Delegate, SessionFaçade, and DTO patterns: if I do not set an ID for the Fagiolo I want to create, the create method launches an exception claiming that no primary key has been given.. But I can see that in the database a row has been added, with a correct new ID (generated automatically by mySQL) and all other fields NULL..
So I would like to know if there is a way to get auto-incrementing keys for EJBs
Thank you!
|
|
|
afogarolli
|
Postato:
31 gennaio 2005
Titolo:
Re: auto-incrementing Primary keys?
|
|
|
In your ejb-jar file you probably have a normal primary key insted of auto-increment (Yes, it's possible!!) so the code will not run if you don't set the primary key. At the db side maybe your mysql configuration uses an autoincrement mechanism if you don't provide a pk. There are a lot of pattern for creating pks. (Try with google) Otherwise if you want to use an auto-increment pk you can use some "plugin". Jboss in its internal offer you this possibility (see the jboss documentation www.jboss.org)
|
|
|
stefano.schivo
|
Postato:
31 gennaio 2005
Titolo:
Re: auto-incrementing Primary keys?
|
|
|
Thank you very much, I will give it a look!
|
|
|
S_andrew
|
Postato:
10 febbraio 2005
Titolo:
Re: Re: auto-incrementing Primary keys?
|
|
|
|
You can generate primary keys, with the help of another one CMP EntityBean. In this bean you can store name of the bean, for which neccessary to generate pk, and the current value of primary key for this bean. If your implement small stateless facade to this Counter Bean, you can very easy call it during ejbCreate method of Bean, for which you add pk, and put the right value of PK.
|
|
|
stefano.schivo
|
Postato:
10 febbraio 2005
Titolo:
Re: auto-incrementing Primary keys?
|
|
|
Wow, cool!! Thanks!!!
|
|
|
afogarolli
|
Postato:
11 febbraio 2005
Titolo:
Re: auto-incrementing Primary keys?
|
|
|
|
Be careful!! The pattern in the way it's described on this tread has no sense. The pattern is called "Sequence block" and you have to read keys from the db in a bulk not every time you need a new primary key. I send you a book about in chapter 5 discuss strategies for primary keys generation . BTW it's the most common pattern used in ejb-world for generating primary keys...
|
|
Allegato:
[
ejbdesignpatterns.pdf
(
929870
bytes)]
|
|
|