Discussions

General J2EE: Standard UID (GUID?) Generation for entity bean?

  1. Standard UID (GUID?) Generation for entity bean? (3 messages)

    I have an entity bean (CMP) I need to design which must have a primary key field generated automatically, which is independent of the data stored in the entity.

    I (at least for now) will be using JBoss. Is there a standard way in java to create a unique string, like GUID stuff in windows?

    Am I thinking about this wrong? Should I be doing something else instead? I am moving a PHP web application to J2EE, and will be loosing the auto incrementing column DB feature if I do this so it is DB independent.

    Thanks for any help,

    -Pete
  2. <I have an entity bean (CMP) I need to design which must have a primary key field generated automatically, which is independent of the data stored in the entity
    >>
    Does this mean at the time of deployment you do not know the primary key field...???
    Or just the data...(Which is ok).in this case you can implement your own PK generator which does this.
  3. I just need a generic PK field, which does not relate to the data being stored. It is for a "Company" entity, in our user management/tracking piece. It should be possible to have identical companies, from all aspects except the PK, which is used to track, and relate to other entities. I just need a generic unique pk.

    It sounds like you are suggesting I write my own PK generator. What I was wondering was if something like that was already implemented as part of the Java libraries, or JBoss.

    Yes, I could write my own, but I'd rather use a "standard" one if such a thing exists.

    -Pete
  4. In that case i shall refer u too

    <Entity Bean Primary Key Generator
    Posted By: Floyd Marinescu on July 13, 2000 on TSS
    **
    * This Entity Bean generates unique id's for any client that needs one.
    *
    * @author Floyd Marinescu
    */
    public class UniqueIDGeneratorBean implements EntityBean {


    protected EntityContext ctx;

    // Environment properties the bean was deployed with
    public Properties env;

    private String idName;
    private long nextID;


    /**
    * Create a PK generator.
    *
    * @param idName the name for this unique id tracker
    * @return The primary key (just a string) for this index
    */
    public UniqueIDGeneratorPK ejbCreate( UniqueIDGeneratorPK key ) throws CreateException, RemoteException
    {
          this.idName = key.idName;
          this.nextID = 0;

          ...

          //Build SQL query
          pstmt = conn.prepareStatement("insert into uniqueIDs (idName, nextID) values (?, ?)");
          pstmt.setString( 1, this.idName);
          pstmt.setLong( 2, this.nextID);
                
          //insert row in databse
          pstmt.executeUpdate();

          return key;

          ...
    }

    public UniqueIDGeneratorPK ejbFindByPrimaryKey(UniqueIDGeneratorPK key) throws FinderException, ObjectNotFoundException, RemoteException
    {
          ...

          // Find the Entity in the DB
          pstmt = conn.prepareStatement("select idName from uniqueIDs where idName = ?");
          pstmt.setString( 1, key.idName );
          rs = pstmt.executeQuery();

          // iterate to the first row in the resultset
          if( ! rs.next() ) //if generator does not exist in database
          {
               throw new ObjectNotFoundException("ID Generator " + key + " does not exist in databse" );
          }

          // No errors occurred, so return the Primary Key
          return key;
          ...
    }

    public void ejbLoad() throws RemoteException
    {
          // Query the Entity Context to get the current
          // Primary Key, so we know which instance to load.
          this.idName = ((UniqueIDGeneratorPK) ctx.getPrimaryKey()).idName;

          ...

          pstmt = conn.prepareStatement("select nextID from uniqueIDs where idName = ?");
          pstmt.setString(1, this.idName);

          rs = pstmt.executeQuery();

          // iterate to the first row in the results
          rs.next();

          // populate with data from database
          this.nextID = rs.getLong("nextID");

          ...
    }

    public void ejbRemove() throws RemoteException
    {
         // Query the Entity Context to get the current
         // Primary Key, so we know which instance to load.
         String pk = ((UniqueIDGeneratorPK)ctx.getPrimaryKey()).idName;
      
         ...

         pstmt = conn.prepareStatement("delete from uniqueIDs where idName = ?");
         pstmt.setString(1, pk);

         //Throw a system-level exception if something bad happened.
         if (pstmt.executeUpdate() == 0)
         {
              //log errors here
              throw new RemoteException("UniqueIdGenerator " + pk + " failed to be removed from the database");
         }

         ...
    }


    public void ejbStore() throws RemoteException
    {
         ...
          
         // Store account in DB
         pstmt = conn.prepareStatement("update uniqueIDs set nextID = ? where idName = ?");

         pstmt.setLong( 1, this.nextID);
         pstmt.setString(2, this.idName);
         pstmt.executeUpdate();

         ...
    }


    public long generateUniqueId() throws EJBException
    {

          this.nextID++;
          return this.nextID;

    }

    The bean is very versatile, and can be used in many different ways. The way I use it is that each entity bean "class" in my application uses a different instance of the UniqueIDGeneratorBean. In the create method of an entity bean class (for example, MessageBean), I would execute:

    UniqueIDGeneratorHome.findByPrimaryKey("MessageBean");

    Which will return an instance of the UniqueIDGeneratorBean that is maintaing count just for my MessageBean class.

    If you want primary keys to be unique across all beans in your application, then simply use one instance of your UniqueIDGenerator across your entire application.
    >>>>