Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - CMP beans - id

 
Thread Tools Search this Thread
Old 12-07-2005, 10:17 AM   #1
Default CMP beans - id


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




Demetz Markus
  Reply With Quote
Old 12-07-2005, 01:30 PM   #2
Viator
 
Posts: n/a
Default Re: CMP beans - id
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



Viator
  Reply With Quote
Old 12-07-2005, 02:47 PM   #3
Bjoern Bredohl
 
Posts: n/a
Default Re: CMP beans - id
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


-----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-----



Bjoern Bredohl
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Scarface - widescreen version easily missed JWB DVD Video 28 10-09-2003 09:48 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46