6 Replies Latest reply on Nov 5, 2004 6:14 AM by Lame Guy

    CMP - composite primary key - @ejb.primkey spec

    Lame Guy Newbie

      Hi, I have this GroupMembershipBean which has a composite primary key:
      1. database field: GROUPUIN (Integer)
      getter: Integer getGroupUIN()
      setter: void setGroupUIN(Integer)
      2. database field: USERUIN (Integer)
      getter: Integer getUserUIN()
      setter: void setUserUIN(Integer)

      My question is, what should I specify in @ejb.bean's "primkey-field" attribute? Can I specify "primkey-field" as a comma-delimited string:

      @ejb.bean
      ...
      primkey-field "GROUPUIN, USERUIN" (Do I speicfy "database field name here?)
      ...
      public abstract class GroupMembershipBean implements EntityBean, Serializable, Comparable {
      ...
      ...
      }


      Then, for GroupUIN and UserUIN getters. specify "@ejb.pk-field" attribute (See code below)

      If not, do I have to implement a "primary key class" and specify "@ejb.pk class" (corresponds to <prim-key-class> in "ejb-jar.xml")? Is this a MUST (can I make do without)?
      public class GroupMembershipKey implements Serializable {
       public Integer GroupUIN;
       public Integer UserUIN;
      
       public GroupMembershipKey() {}
      
       public GroupMembershipKey(Integer GroupUIN, Integer UserUIN) {
       if(GroupUIN==null || UserUIN==null) { throw new Exception(); }
       this.GroupUIN=GroupUIN;
       this.UserUIN=UserUIN;
       return;
       }
      
       public Integer getGroupUIN() {
       return GroupUIN;
       }
      
       public Integer getUserUIN() {
       return UserUIN;
       }
      
       public boolean equals(Object o) {
       GroupMembershipKey key=null;
       if(o instanceof GroupMembershipKey) {
       key=(GroupMembershipKey) o;
       if(GroupUIN.equals(key.getGroupUIN) && UserUIN.equals(key.getUserUIN)) {
       return true;
       } else { return false; }
       } else {
       return false;
       }
       }
      
       public String toString() {
       StringBuffer buf=new StringBuffer("GroupMembershipKey ");
       buf.append(" Group UIN: ").append(GroupUIN);
       buf.append(" User UIN: ").append(UserUIN);
       return buf.toString();
       }
      }


      For reference, here's GroupMembershipBean class:
      /**
       *
       * @ejb.bean
       * name="GroupMembershipEJB"
       * local-jndi-name="ejb/GroupMembership"
       * display-name="Group Membership EJB"
       * description="Group Membership Group EJB"
       * cmp-version="2.x"
       * type="CMP"
       * view-type="local"
       * schema="GroupMembership"
       * reentrant="false"
       * primkey-field="???" What should I specify here?
       *
       * @ejb.persistence
       * table-name="GROUPMEMBERSHIP"
       *
       * @ejb.interface
       * local-extends="javax.ejb.EJBLocalObject, java.lang.Comparable, java.io.Serializable"
       * local-class="com.aa.samples.interfaces.GroupMembershipLocal"
       *
       * @ejb.home
       * local-class="com.aa.samples.interfaces.GroupMembershipLocalHome"
       *
       * @jboss.persistence
       * datasource="java:/jdbc/dev01"
       * datasource-mapping="mySQL"
       * create-table="false"
       * remove-table="false"
       */
      public abstract class GroupMembershipBean implements EntityBean, Serializable, Comparable {
       ... other stuff ...
      
       /*
       * Persistence methods:
       */
      
       /**
       * @ejb.persistent-field
       * @ejb.persistence
       * column-name="GROUPUIN"
       * sql-type="INTEGER"
       * @ejb.pk-field
       * @ejb.interface-method
       */
       public abstract Integer getGroupUIN();
      
       /**
       * @ejb.interface-method
       */
       public abstract void setGroupUIN(Integer groupUIN);
      
       /**
       * @ejb.persistent-field
       * @ejb.persistence
       * column-name="USERUIN"
       * sql-type="INTEGER"
       * @ejb.pk-field
       * @ejb.interface-method
       */
       public abstract Integer getUserUIN();
      
       /**
       * @ejb.interface-method
       */
       public abstract void setUserUIN(Integer userUIN);
      
       ... other stuff ...
      
      }


      Thanks in advance!

        • 1. Re: CMP - composite primary key - @ejb.primkey spec
          Lame Guy Newbie

          Think I found part of the answer (Still need to know what XDoclet tags I need to specify) - Quoting J2EE Tutorial from Sun:

          Chapter: "Primary Keys for Container-Managed Persistence"

          If the primary key class does not belong to the J2SE or J2EE standard libraries, then you must implement the class and package it along with the entity bean. For example, if your entity bean requires a composite primary key (which is made up of multiple fields), then you need to provide a customized primary key class.

          • 2. Re: CMP - composite primary key - @ejb.primkey spec
            Lame Guy Newbie

            So, I've implemented a primary key class "GroupMembershipKey" -- I still need to know what I should specify for @ejb.bean primkey-field attribute.

            My understanding is that @ejb.bean primkey-field is not mandatory. Will it suffice if I just specify @ejb.pk-field on primary key getters? Example:

             /**
             * @ejb.persistent-field
             * @ejb.persistence
             * column-name="GROUPUIN"
             * sql-type="INTEGER"
             * @ejb.pk-field
             * @ejb.interface-method
             */
             public abstract Integer getGroupUIN();
            
             NOTE: Same for getUserUIN()
            


            And ...
            /**
             *
             * @ejb.bean
             * name="GroupMembershipEJB"
             * local-jndi-name="ejb/GroupMembership"
             * display-name="Group Membership EJB"
             * description="Group Membership Group EJB"
             * cmp-version="2.x"
             * type="CMP"
             * view-type="local"
             * schema="GroupMembership"
             * reentrant="false"
             * primkey-field="???"
             *
             * @ejb.persistence
             * table-name="GROUPMEMBERSHIP"
             *
             * @ejb.pk
             * class="com.aa.samples.primarykeys.GroupMembershipKey"
             *
             * @ejb.interface
             * local-extends="javax.ejb.EJBLocalObject, java.lang.Comparable, java.io.Serializable"
             * local-class="com.aa.samples.interfaces.GroupMembershipLocal"
             *
             * @ejb.home
             * local-class="com.aa.samples.interfaces.GroupMembershipLocalHome"
             *
             * @jboss.persistence
             * datasource="java:/jdbc/dev01"
             * datasource-mapping="mySQL"
             * create-table="false"
             * remove-table="false"
             */
            public abstract class GroupMembershipBean implements EntityBean, Serializable, Comparable {
             ...
            }
            


            • 3. Re: CMP - composite primary key - @ejb.primkey spec
              Russ Loucks Newbie

              I found that one cannot specify the 'primkey-field' attribute on the @ejb.bean tag if you have
              a composite key for the bean. You must specify @ejb.pk-field tags on all of the getter/setter
              methods that define your composite key.

              Then, if you specify the <entitypk ....> subtask in the <xdoclet ...> task in your build file, the
              appropriate 'xxxPK.java' class will be generated for you with the fields you specify as the composite key.

              • 4. Re: CMP - composite primary key - @ejb.primkey spec
                Lame Guy Newbie

                Thanks, I'm stiill reading up on one article: http://www-106.ibm.com/developerworks/library/j-santa1/

                Anyway, yes, i did put @ejb.pk-field method level tags on getters. But do we need @ejb.bean primkey-field still? I guess we do - but we specify "field name" - NOT "column name".

                so... here's what I have in mind, not yet put to test however:

                /**
                 ...
                 @ejb.bean
                 ...
                 primkey-field="PK" QUESTION 1: Do I still need this attribute with composite key class?
                 ...
                */
                class abstract GroupMembershipBean {
                
                 ...
                 /**
                 * @ejb.interface-method
                 * QUESTION 2: DO I NEED "@ejb.pk-field" HERE?
                 */
                 public abstract GroupMembershipKey getPK( );
                 ...
                
                 /**
                 * @ejb.persistent-field
                 * @ejb.persistence
                 * column-name="GROUPUIN"
                 * sql-type="INTEGER"
                 * @ejb.pk-field
                 * @ejb.interface-method
                 */
                 public abstract Integer getGroupUIN();
                
                 /**
                 * @ejb.interface-method
                 */
                 public abstract void setGroupUIN(Integer groupUIN);
                
                 /**
                 * @ejb.persistent-field
                 * @ejb.persistence
                 * column-name="USERUIN"
                 * sql-type="INTEGER"
                 * @ejb.pk-field
                 * @ejb.interface-method
                 */
                 public abstract Integer getUserUIN();
                
                 /**
                 * @ejb.interface-method
                 */
                 public abstract void setUserUIN(Integer userUIN);
                }
                
                


                • 5. Re: CMP - composite primary key - @ejb.primkey spec
                  Russ Loucks Newbie

                  No, do not specify the @ejb.bean primkey-field tag/attribute for beans with composite keys.

                  Only specify the @ejb.pk-field tag on the getters/setters of the fields corresponding to your
                  composite key. Do not use it in your 'getPK()' method.

                  • 6. Re: CMP - composite primary key - @ejb.primkey spec
                    Lame Guy Newbie

                    Thanks Red, I got that part working now.