May I implement version number pattern in primary key?

Discussions

EJB design: May I implement version number pattern in primary key?

  1. In "EJB Design Patterns: Advanced Patterns, Processes, and Idioms" book, Floyd proposed version number pattern. As I understand, I have to add one extra column for version number in each table if I want to implement it. May I implement it in primary key?

    I need clarification about the following question.

    The specification said 'it is possible that multiple entity beans use the same primary key class' on section 9.8.

    Now I have two entity bean that uses the same primary key as indicated by the following codes:

    -------------------------------------Primary Key object---------------------------------------
    public class IDPrimaryKey {
        private String id = null;
        private Integer versionNumber = null;

        public void setID(String newID) {
    this.id = newID;
        }

        public String getID() {
            return id;
        }

        public boolean equals(Object obj) {
            if (obj instanceof this) {
    MyPrimaryKey temp = (MyPrimaryKey)obj;
    return id.equals(temp.getID));
    }

            return false;
        }


        public int hashCode() {
    return id.hashCode();
        }

        public Integer getVersionNumber() {return versionNumber;}
        public void setVersionNumber(Integer newVersionNumber) {this.versionNumber = newVersionNumber;}
    }

    -------------------------------------------------Entity bean 1-----------------------------------------

    public class Employee implements EntityBean {
          private int versionNumberCount = 0;
    private IDPrimaryKey primaryKey = null;
          private EntityContext entityContext = null;

          
    public void ejbActivate() throws EJBException,java.rmi.RemoteException {
    primaryKey = (IDPrimaryKey)entityContext.getPrimaryKey();
          }

    public void ejbLoad() throws EJBException,java.rmi.RemoteException {
              ...
              ... load data from database.........
         
    primaryKey.setVersionNumber(new Integer(versionNumberCount++));
          }
          ................

    }

    -------------------------------------------------Entity Bean 2 -------------------------------------
    public class Student implements EntityBean {
    private IDPrimaryKey primaryKey = null;
          private EntityContext entityContext = null;

          
    public void ejbActivate() throws EJBException,java.rmi.RemoteException {
    primaryKey = (IDPrimaryKey)entityContext.getPrimaryKey();
          }

          ................
          public void ejbLoad() throws EJBException,java.rmi.RemoteException {
              ...
              ... load data from database.........don't set version number here
          }
    }

    ----------------------------------------------------------------------------------------------

    If I am debuging them, two IDPrimaryKey objects in above two entity beans have different memory
    address although memeber attribute 'id' is the same among two IDPrimaryKey objects and one in
    Employee entity bean with non-null versionNumber and one in Student entity bean with null versionNumber.

    What happens if the ejb container swap them out and bring them back sometime later?
    Can Employee entity bean get IDPrimaryKey object with non-null versionNumber
    and Student entity bean get IDPriamryKey object with null versionNumber?


    Jim
  2. To me, primary key is a unique identifier that will never change in database, while version number increases with each update. How can you combine them?
    Stefan
  3. I don't want to add one extra column for version number in database table. I just want to add version number to primary key object as one attribute. However, I don't use version number in equals and hashCode method.

    Cheers!

    Jim
  4. The idea is interesting, but I don't think it would work.

    1. By doing this you expose yourself to the risk of having two primary key objects which would "answer" equals() true, when in fact they have different version numbers!

    2. You assume that the EJB container produces only one entity bean for same primary key. I have seen app servers that will provide as part of the "entity bean pool" two instances for same primary key! To make them "behave as one" you have to set an exclusive lock in configuration ...

    Cheers,
    Stefan