CMP beans - id

D

Demetz Markus

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
 
V

Viator

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 :)
 
B

Bjoern Bredohl

Demetz said:
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


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: GnuPT 2.7.2

iD8DBQFDlvX2BI/akcB2BtkRAu+fAJ4zFvTXbW/6L+jITfpuaQjz0hiL0QCfcwe9
55HGDkmJ4ZmSK6B2TpWn96o=
=L/0n
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top