4 Replies Latest reply on Mar 17, 2004 3:42 PM by jorge middleton

    compound Primary Key

    rfeenstr Newbie

      Does anyone have an example of a JBoss 3.2 implementation of an CMP entity bean that needs a compound primary key (and thus a PK class)?
      JBoss is finding my PK class (class is instantiated (0-arg constructor) during deployment of my entity bean JAR, but the resultant SQL has no field/column names or values.

      Any ideas, or working examples I can look at? ejb-jar.xml, java code, etc.

      TIA

        • 1. Re: compound Primary Key
          Gustavo Comba Newbie

          Hi,

          Make sure your PK class adjusts to the following:

          * ALL the class variables corresponding to your PK MUST BE public

          * The class variables names MUST BE EXACTLTY the same you have defined in ejb-jar.xml:

           <cmp-field>
           <field-name>field_name</field-name>
           </cmp-field>
          


          * Implements java.io.Serializable

          * Has a public default constructor

          * Implements a "good" int hashCode()

          * Implements a goog boolean equals(Object o)

          * Optionally: implements a good String toString()

          Also, in your ejb-jar.xml, make sure that:

          * You specify the PK class's FULL NAME in <prim-key-class>, example:

          <prim-key-class>net.infinitumdoor.explot.usuarios.ejb.UsuarioEBPK</prim-key-class>
          


          * Don't include any <primkey-field> for your EB

          That's all... I could miss something, but these are the general rules. Good luck!

          Gustavo Comba

          • 2. Re: compound Primary Key
            rfeenstr Newbie

            Thanks much, gcomba. Currently side-tracked with the crisis-du-jour, but I'll try your suggestions out as soon as I can.

            • 3. Re: compound Primary Key
              ebende Newbie

              Hi guys,

              I did exactly what Gustavo said some time ago as far as compound primary keys in EJBs are concerned.
              Yet I get the deployment error listed below from Jboss-3.2.2.
              Do I need to configure someting in JBoss in addition?
              For CMP EJBs with single PKs everything works fine.

              Hope you can help me. Thanks,

              Evert Bende


              My codes for my test bean ("A") with corresponding DB table ("a") are listed below.
              The PK-class was generated by Borland JBuilderX.
              My table is stored in MySQL.
              None of the pks in table "a" are auto-incremented.

              //////////////////////////////////////////
              Deployment Error from JBOSS-3.2.2
              //////////////////////////////////////////
              2004-03-04 11:37:08,501 ERROR [org.jboss.ejb.EntityContainer] Starting failed
              org.jboss.deployment.DeploymentException: Generation only supported with single PK field
              at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.getGeneratedPKField(JDBCAbstractCreateCommand.java:171)
              at org.jboss.ejb.plugins.cmp.jdbc.JDBCIdentityColumnCreateCommand.initGeneratedFields(JDBCIdentityColumnCreateCommand.java:46)
              at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.init(JDBCAbstractCreateCommand.java:91)
              at org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCMySQLCreateCommand.init(JDBCMySQLCreateCommand.java:40)
              at org.jboss.ejb.plugins.cmp.jdbc.JDBCCommandFactory.createCreateEntityCommand(JDBCCommandFactory.java:128)
              at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.startStoreManager(JDBCStoreManager.java:427)
              at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.start(JDBCStoreManager.java:351)
              at org.jboss.ejb.plugins.CMPPersistenceManager.start(CMPPersistenceManager.java:152)
              at org.jboss.ejb.EntityContainer.startService(EntityContainer.java:343)
              at org.jboss.system.ServiceMBeanSupport.start(ServiceMBeanSupport.java:192)
              at sun.reflect.GeneratedMethodAccessor133.invoke(Unknown Source)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              //////////////////////////////////////////
              ABean
              //////////////////////////////////////////
              package com.wattabout.wattsbest.ejb.cmp;
              import javax.ejb.EntityBean;
              import javax.ejb.EntityContext;
              import javax.ejb.CreateException;
              import javax.ejb.RemoveException;
              public abstract class ABean
              implements EntityBean {
              EntityContext entityContext;
              public APK ejbCreate(Integer apk1, Integer apk2) throws CreateException {
              setApk1(apk1);
              setApk2(apk2);
              return null;
              }
              public void ejbPostCreate(Integer apk1, Integer apk2) throws CreateException { }
              public void ejbRemove() throws RemoveException { }
              public abstract void setAfld(String afld);
              public abstract void setApk1(Integer apk1);
              public abstract void setApk2(Integer apk2);
              public abstract String getAfld();
              public abstract Integer getApk1();
              public abstract Integer getApk2();
              public void ejbLoad() { }
              public void ejbStore() { }
              public void ejbActivate() { }
              public void ejbPassivate() { }
              public void setEntityContext(EntityContext entityContext) { this.entityContext = entityContext; }
              public void unsetEntityContext() { this.entityContext = null; }
              }
              //////////////////////////////////////////
              A
              //////////////////////////////////////////
              package com.wattabout.wattsbest.ejb.cmp;
              import javax.ejb.EJBLocalObject;
              public interface A extends EJBLocalObject {
              public void setAfld(String afld);
              public String getAfld();
              public Integer getApk1();
              public Integer getApk2();
              }
              //////////////////////////////////////////
              AHome
              //////////////////////////////////////////
              package com.wattabout.wattsbest.ejb.cmp;
              import javax.ejb.EJBLocalHome;
              import javax.ejb.CreateException;
              import javax.ejb.FinderException;
              public interface AHome extends EJBLocalHome {
              public A create(Integer apk1, Integer apk2) throws CreateException;
              public A findByPrimaryKey(APK pk) throws FinderException;
              }
              //////////////////////////////////////////
              APK
              //////////////////////////////////////////
              package com.wattabout.wattsbest.ejb.cmp;
              import java.io.Serializable;
              public class APK
              implements Serializable {
              public Integer apk1;
              public Integer apk2;
              public APK() {
              }
              public APK(Integer apk1, Integer apk2) {
              this.apk1 = apk1;
              this.apk2 = apk2;
              }
              public boolean equals(Object obj) {
              if (this == obj) {
              return true;
              }
              if (! (obj instanceof APK)) {
              return false;
              }
              APK that = (APK) obj;
              if (! (that.apk1 == null ? this.apk1 == null : that.apk1.equals(this.apk1))) {
              return false;
              }
              if (! (that.apk2 == null ? this.apk2 == null : that.apk2.equals(this.apk2))) {
              return false;
              }
              return true;
              }
              public int hashCode() {
              int result = 17;
              result = 37 * result + this.apk1.hashCode();
              result = 37 * result + this.apk2.hashCode();
              return result;
              }
              }
              //////////////////////////////////////////
              ejb-jar.xml
              //////////////////////////////////////////

              <display-name>A</display-name>
              <ejb-name>A</ejb-name>
              <local-home>com.wattabout.wattsbest.ejb.cmp.AHome</local-home>
              com.wattabout.wattsbest.ejb.cmp.A
              <ejb-class>com.wattabout.wattsbest.ejb.cmp.ABean</ejb-class>
              <persistence-type>Container</persistence-type>
              <prim-key-class>com.wattabout.wattsbest.ejb.cmp.APK</prim-key-class>
              False
              <cmp-version>2.x</cmp-version>
              <abstract-schema-name>A</abstract-schema-name>
              <cmp-field>
              <field-name>afld</field-name>
              </cmp-field>
              <cmp-field>
              <field-name>apk1</field-name>
              </cmp-field>
              <cmp-field>
              <field-name>apk2</field-name>
              </cmp-field>

              //////////////////////////////////////////
              jbosscmp-jdbc.xml
              //////////////////////////////////////////

              <ejb-name>A</ejb-name>
              <table-name>a</table-name>
              <cmp-field>
              <field-name>afld</field-name>
              <column-name>afld</column-name>
              </cmp-field>
              <cmp-field>
              <field-name>apk1</field-name>
              <column-name>apk1</column-name>
              </cmp-field>
              <cmp-field>
              <field-name>apk2</field-name>
              <column-name>apk2</column-name>
              </cmp-field>

              • 4. Re: compound Primary Key
                jorge middleton Newbie

                I having the same problem in Jboss 3.2.3... Someone could tell me how to solve this.

                Thank in advance