users@glassfish.java.net

RE: Entity auto generated id

From: Gordon Yorke <gordon.yorke_at_oracle.com>
Date: Wed, 24 Jan 2007 11:20:01 -0500

Rabid Dog Labs SignatureHello Kenneth,
    Yes a GUID can be used as the PK value for an entity. I would recomend storing it in the Entity in the form of a string.
    How you get the GUID generated and into the Entity is a much tougher problem. If you are on a database that supports GUID generation through the native sequence mechanism then configure the sequence on the database and configure TopLink to use that sequence through the Java Persistence APIs.
    The easiest alternative would be to have your application generate the GUIDs and set them in the Entity on creation of the Entity or within a prePersist event.
    You could create a GUID Sequence type for TopLink, create a sequence using this new Sequence Type and configure the TopLink descriptor to use this sequence.
    If you are on a database that supports identity fields you could write a trigger that generates the GUID on insert and configure TopLink to use IDENTITY sequencing through the Java Persistence APIs.
    If all of your classes would be using GUIDs on a database that does not support identity fields the database platform in TopLink could be configured to support trigger generated GUIDs through custom configuration.
--Gordon
  -----Original Message-----
  From: Kenneth Clark [mailto:kenneth_at_rabiddog.co.za]
  Sent: Wednesday, January 24, 2007 6:47 AM
  To: users_at_glassfish.dev.java.net
  Subject: Entity auto generated id


  This might be a really weird question so please forgive me

  Is there anyway to auto generate a unique ID using a custom routine for the record?

  i.e.: I would like to use a GUID as the PK value for an entity. Is this possible?

  Thanks


  --
  Kenneth Clark
  Developer / Analyst

  Rabid Dog Laboratories T
  Putting the art back into development

  tel: +27 11 475 7409
  mobile: +27 82 500 5090
  e-mail: kenneth_at_rabiddog.co.za
  website: http://www.rabiddog.co.za/