CMP beans - id

Discussion in 'Java' started by Demetz Markus, Dec 7, 2005.

  1. 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, Dec 7, 2005
    #1
    1. Advertising

  2. Demetz Markus

    Viator Guest

    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, Dec 7, 2005
    #2
    1. Advertising

  3. 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, Dec 7, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim

    Entity beans and CMP

    Tim, Jan 30, 2004, in forum: Java
    Replies:
    0
    Views:
    341
  2. Andrea Sansottera

    no cmp field defined in cmp ejb

    Andrea Sansottera, Jul 16, 2004, in forum: Java
    Replies:
    0
    Views:
    408
    Andrea Sansottera
    Jul 16, 2004
  3. Tobias Merler
    Replies:
    1
    Views:
    3,174
    Sudsy
    Sep 8, 2004
  4. Replies:
    1
    Views:
    303
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Jul 21, 2007
  5. Lord0
    Replies:
    4
    Views:
    460
    Lord0
    Sep 8, 2008
Loading...

Share This Page