1 Reply Latest reply on Nov 23, 2001 12:53 AM by Erik Sliman

    composite PK problem

    francesco milesi Newbie

      I am using Jboss2.4.1.

      I have a CMP bean with one field myPK of type MyPK that is the PK.
      MyPK override equals and hashCode as usual and has two public fields field1 and field2.

      In ejb-jar.xml I put:


      <ejb-name>MyBean</ejb-name>
      my.MyBeanHome
      my.MyBean
      <ejb-class>my.MyBeanCMP</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>my.MyPK</prim-key-class>
      False
      <cmp-field><field-name>myPK</field-name></cmp-field>
      <primkey-field>myPK</primkey-field>



      and in jaws.xml:


      <ejb-name>MyBean</ejb-name>
      <table-name>myTable</table-name>
      <create-table>false</create-table>
      <cmp-field>
      <field-name>myPK.field1</field-name>
      <column-name>FIELD1</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>myPK.field2</field-name>
      <column-name>FIELD2</column-name>
      </cmp-field>



      When I try to create a record I get an error from my jdbc driver that says that the column myPK doas not exists.

      I don't see where is the error in the mapping of fields. I have also tried a different approch in jaws
      putting:


      <ejb-name>MyBean</ejb-name>
      <table-name>myTable</table-name>
      <create-table>false</create-table>
      <cmp-field>
      <field-name>field1</field-name>
      <column-name>FIELD1</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>field2</field-name>
      <column-name>FIELD2</column-name>
      </cmp-field>



      but it did not work.

      I have read other posting on the subject and tried to do as suggested there but it did not work.

      Please please please help.

      Thanks in advance, Francesco

        • 1. Re: composite PK problem
          Erik Sliman Apprentice

          Your latter JAWS.XML example is correct; the field names should be field1 and field2, not myPK.

          In your EJB-JAR.XML, leave the primary-key tag blank.

          Here is a sample EJB-JAR entry:
          [pre]

          RoleModule EJB
          <ejb-name>RoleModuleBean</ejb-name>
          net.openstandards.ejb.entity.security.RoleModuleHome
          net.openstandards.ejb.entity.security.RoleModule
          <ejb-class>net.openstandards.ejb.entity.security.RoleModuleBean</ejb-class>
          <persistence-type>Container</persistence-type>
          <prim-key-class>net.openstandards.ejb.entity.security.RoleModuleKey</prim-key-class>
          False
          <cmp-field><field-name>roleId</field-name></cmp-field>
          <cmp-field><field-name>moduleId</field-name></cmp-field>
          <cmp-field><field-name>canCreate</field-name></cmp-field>
          <cmp-field><field-name>canRetrieve</field-name></cmp-field>
          <cmp-field><field-name>canUpdate</field-name></cmp-field>
          <cmp-field><field-name>canDelete</field-name></cmp-field>
          <primkey-field></primkey-field>

          [/pre]
          Here is the JAWS.XML entry:
          [pre]

          <ejb-name>RoleModuleBean</ejb-name>
          <table-name>RoleModules</table-name>
          <pk-constraint>true</pk-constraint>
          <cmp-field>
          <field-name>roleId</field-name>
          <column-name>roleId</column-name>
          <jdbc-type>INTEGER</jdbc-type>
          <sql-type>INTEGER</sql-type>
          </cmp-field>
          <cmp-field>
          <field-name>moduleId</field-name>
          <column-name>moduleId</column-name>
          <jdbc-type>INTEGER</jdbc-type>
          <sql-type>INTEGER</sql-type>
          </cmp-field>
          <cmp-field>
          <field-name>canCreate</field-name>
          <column-name>canCreate</column-name>
          <jdbc-type>VARCHAR</jdbc-type>
          <sql-type>CHAR(1)</sql-type>
          </cmp-field>
          <cmp-field>
          <field-name>canRetrieve</field-name>
          <column-name>canRetrieve</column-name>
          <jdbc-type>VARCHAR</jdbc-type>
          <sql-type>CHAR(1)</sql-type>
          </cmp-field>
          <cmp-field>
          <field-name>canUpdate</field-name>
          <column-name>canUpdate</column-name>
          <jdbc-type>VARCHAR</jdbc-type>
          <sql-type>CHAR(1)</sql-type>
          </cmp-field>
          <cmp-field>
          <field-name>canDelete</field-name>
          <column-name>canDelete</column-name>
          <jdbc-type>VARCHAR</jdbc-type>
          <sql-type>CHAR(1)</sql-type>
          </cmp-field>

          [/pre]
          Here is the composite key class:
          [pre]
          public class RoleModuleKey implements java.io.Serializable {

          public Integer roleId, moduleId ;

          /** Creates new RoleKey */
          public RoleModuleKey() { }
          public RoleModuleKey(Integer _roleId, Integer _moduleId) {
          roleId = _roleId ;
          moduleId = _moduleId ;
          }

          public Integer getRoleId() { return roleId; }
          public Integer getModuleId() { return moduleId; }

          public boolean equals(Object other) {
          if (other instanceof RoleModuleKey) {
          return (
          roleId.equals( ((RoleModuleKey) other).roleId) &&
          moduleId.equals( ((RoleModuleKey) other).moduleId)
          // this.hashCode() == ((RoleModuleKey) other).hashCode()
          );
          }
          return false;
          }

          public int hashCode() {
          String sResult = roleId.toString() + "," + moduleId.toString() ;
          return sResult.hashCode();
          }

          }
          [/pre]