Hi everybody!
I'm new to CMP Beans, and i want each bean to have a unique id and do the
relationships over id's. But which is the best way to generate id's? I found
the following:
private Integer generatePrimaryKey() {
return new Integer((new Object()).hashCode());
}
is this a good way?
Isn't there another approach to do an autoincrement?
Thanks for any hint!
Markus
Viator - 07 Dec 2005 13:30 GMT
There is no guarantee that new Object().hashCode() will always give you
unique numbers. Try using a class (some sort of Sequencer) that gives
you a unique integer and increments it by one with every call.
Amit :-)
Bjoern Bredohl - 07 Dec 2005 14:47 GMT
Demetz Markus schrieb:
> private Integer generatePrimaryKey() {
> return new Integer((new Object()).hashCode());
> }
You can use this in jdk 1.5.x
...
setId(java.util.UUID.randomUUID().toString());
...
You can also look for UUID Generator like JUG (http://jug.safehaus.org/Home)
> Isn't there another approach to do an autoincrement?
Yes, there is another possibility. The RDB can autoincrement. But if you
have CMR's you get problems.
Björn