8 Replies Latest reply on Nov 14, 2002 4:51 PM by Brad

    CMP muitple primary key~

    Mike Newbie

      I am trying to build a EJB with mutliple primary key in jboss,

      i know how to set jaws.xml, ejb-jar.xml, and develop PK class,

      but i don't know what to deal with the Bean, local , remote Home, such as how to set the value of primary key...

      does anyone have a complete example for CMP EJB with multiple primary key using JBOSS?

        • 1. Re: CMP muitple primary key~
          Mike Newbie

          I try my best to make the EJB work,
          here is my code, pls help~

          i am using jboss-3.0.0_tomcat-4.0.3

          standardjaws.xml
          <enterprise-beans>

          <ejb-name>ShippingScheduleEJB</ejb-name>
          <table-name>ShippingSchedule</table-name>
          <pk-constraint>true</pk-constraint>
          <cmp-field>
          <field-name>scheduleNumber</field-name>
          <column-name>scheduleNumber</column-name>
          <jdbc-type>CHAR</jdbc-type>
          <sql-type>CHAR(15)</sql-type>
          </cmp-field>
          <cmp-field>
          <field-name>batSeq</field-name>
          <column-name>batSeq</column-name>
          <jdbc-type>CHAR</jdbc-type>
          <sql-type>CHAR(5)</sql-type>
          </cmp-field>

          </enterprise-beans>


          ShippingScheduleBean.java
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
          import javax.ejb.EntityContext;
          import java.rmi.RemoteException;
          import javax.ejb.CreateException;
          import java.sql.Date;

          public abstract class ShippingScheduleBean implements javax.ejb.EntityBean
          { protected EntityContext ctx;

          public ShippingSchedulePK ejbCreate(String ScheduleNumber, String batSeq, String carrier) throws CreateException
          { System.out.println("ShippingSchedule Bean " + scheduleNumber + " create");

          this.setShippingSchedulePK(scheduleNumber, batSeq);
          this.setCarrier(carrier);
          }

          public void ejbPostCreate(String scheduleNumber, String carrier){}

          public abstract ShippingSchedulePK getShippingSchedulePK();
          public abstract String getCarrier();

          public abstract void setShippingSchedulePK(String scheduleNumber, String batSeq);
          public abstract void setCarrier(String carrier);

          ....
          }


          ShippingScheduleHomeRemote.java
          public interface ShippingScheduleHomeRemote extends javax.ejb.EJBHome {

          public ShippingScheduleRemote create(String ScheduleNumber, String batSeq, String carrier) throws RemoteException, CreateException;

          public ShippingScheduleRemote findByPrimaryKey (ShippingSchedulePK shippingSchedulePK) throws RemoteException, FinderException;

          public Collection findAll() throws RemoteException, FinderException;
          }

          ShippingScheduleRemote.java
          public interface ShippingScheduleRemote extends javax.ejb.EJBObject
          { public ShippingSchedulePK getShippingSchedulePK() throws RemoteException;
          public void setShippingSchedulePK(String scheduleNumber, String batSeq) throws RemoteException;

          public String getCarrier() throws RemoteException;
          public void setCarrier(String carrier) throws RemoteException;
          }


          import java.io.Serializable;
          public class ShippingSchedulePK implements java.io.Serializable
          { public String scheduleNumber;
          public String batSeq;

          public ShippingSchedulePK(String scheduleNumber, String batSeq)
          { this.scheduleNumber = scheduleNumber;
          this.batSeq = batSeq;
          }

          public ShippingSchedulePK()
          {}

          public int hashCode()
          { return this.toString().hashCode();
          }

          public boolean equals(Object o)
          { if (o instanceof ShippingSchedulePK)
          { return toString().equals(((ShippingSchedulePK)o).toString());
          }
          else
          { return false;
          }
          }

          public String toString()
          { String strConcat = new String(scheduleNumber.trim() + "|" + batSeq.trim());

          return strConcat;
          }
          }

          ejb-jar.xml
          ...

          <ejb-name>ShippingScheduleEJB</ejb-name>
          ShippingScheduleHomeRemote
          ShippingScheduleRemote
          <ejb-class>ShippingScheduleBean</ejb-class>
          <persistence-type>Container</persistence-type>
          <prim-key-class>ShippingSchedulePK</prim-key-class>
          False
          <cmp-version>2.x</cmp-version>
          <abstract-schema-name>ShippingScheduleSchema</abstract-schema-name>
          <cmp-field><field-name>carrier</field-name></cmp-field>
          <primkey-field>scheduleNumber</primkey-field-->
          <security-identity><use-caller-identity/></security-identity>
          <resource-ref>
          DataSource for the test database
          <res-ref-name>jdbc/MySqlDS</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
          </resource-ref>

          <query-method>
          <method-name>findAll</method-name>
          <method-params/>
          </query-method>
          <ejb-ql>SELECT Object(shippingSchedule) FROM ShippingScheduleSchema AS shippingSchedule</ejb-ql>


          ...

          jbosscmp-jdbc.xml
          ...

          <ejb-name>ShippingScheduleEJB</ejb-name>
          <table-name>ShippingSchedule</table-name>
          <cmp-field>
          <field-name>carrier</field-name>
          <column-name>carrier</column-name>
          <jdbc-type>CHAR</jdbc-type>
          <sql-type>CHAR(15)</sql-type>
          </cmp-field>

          ...

          AND this is some of the error message that i receive in jboss

          10:00:47,970 ERROR [EJBDeployer] Could not deploy file:/usr/local/jboss-3.0.0_tomcat-4.0.3/server/default/deploy/logon.jar
          org.jboss.deployment.DeploymentException: Error while creating table; - nested throwable: (java.sql.SQLException: Syntax error or access violation: You have an error in your SQL syntax near '))' at line 1)




          • 2. Re: CMP muitple primary key~
            Stephen Coy Novice

            This looks like you're using CMP 2.0, therefore you need to use jbosscmp-jdbc.xml, rather than jaws.xml.

            jaws.xml is for backward compatibility with CMP 1.1 applications that were deployed under JBoss 2.4.

            Also, make sure that you are using the latest JBoss distribution (3.0.3).

            Finally, if you're just starting out it is probably not worth bothering with tomcat. Use the built in Jetty server instead - it's easier and some would say better.

            Steve

            • 3. Re: CMP muitple primary key~
              Mike Newbie

              yes, i also set the jbosscmp-jdbc.xml,
              but how can i set compound key for just using jbosscmp-jdbc.xml

              just set the primary-key-class is ok?

              • 4. Re: CMP muitple primary key~
                Stephen Coy Novice

                ShippingScheduleBean needs abstract getters/setters for scheduleNumber and batSeq, which must correspond exactly according to normal javabeans conventions with the public fields in ShippingSchedulePK. Then simply call each setter in your ejbCreate method.

                In other words, you need:

                setScheduleNumber
                setBatSeq

                instead of:

                setShippingSchedulePK

                Your ejbPostCreate method *must* have the same argument signature as ejbCreate.

                Steve

                • 5. Re: CMP muitple primary key~
                  Mike Newbie

                  does you mean just config the jbosscmp-jdbc.xml inside the /WEB-INF/?

                  do i need to add something inside jboss/server/default/conf/standardjbosscmp-jdbc.xml,
                  and standardjaws.xml????

                  • 6. Re: CMP muitple primary key~
                    Stephen Coy Novice

                    Forget anything to do with jaws. It's irrelevant to you.
                    You also don't need to touch standardjbosscmp-jdbc.xml.

                    At the moment, the problem is in your java code. Get that right first.

                    Steve

                    • 7. Re: CMP muitple primary key~
                      Mike Newbie

                      OH, thanks~
                      it works now!

                      for me, it is quite strange the how can the xml config match to the PK class...

                      anyway, it works...
                      thank you very much!

                      • 8. Re: CMP muitple primary key~
                        Brad Newbie

                        Congratulations!
                        I also have a same problem to build multiple primary keys as you met.
                        However it doesn't work thought I read this and followed.
                        Could you send the sample you were success?

                        Many thanks.