4 Replies Latest reply on Nov 21, 2001 11:55 AM by mjlarsen

    Composite PK in CMP

    parshuram

      Hi All,
      I am trying to use composite Primary Key in CMP Jboss Container with tomcat server .Can anyone having ideas
      About how to use Composite PK in CMP.
      Also suggest me how to define primarykey-field tag in xml.
      If so please suggest me. Also send me URL where these topics material are to be found.

        • 1. Re: Composite PK in CMP
          nhebert

          Parshuram,

          Your problem with a composite PK using CMP can be
          solved by creating a PK class. As a matter of best
          practice I always create a PK class even if the PK
          for the entity bean can be satisfied by a single
          CMP field.

          Just make sure that the PK class implements
          java.io.Serializable and overrides equals() and
          hashCode()

          Here is an example:


          package StockTradingDataManagers;

          import java.io.*;

          public class HoldingPK implements Serializable {

          public Integer customerID;
          public String name;

          public HoldingPK() {
          }

          public HoldingPK(Integer pCustomerID, String pName) {
          this.customerID = pCustomerID;
          this.name = pName;
          }

          public int hashCode() {
          return (this.customerID.hashCode() + this.name.hashCode());
          }

          public boolean equals(Object pObject) {
          if (pObject == null || !(pObject instanceof HoldingPK))
          return false;
          else if (this.name.equalsIgnoreCase(((HoldingPK)pObject).name) &&
          this.customerID.compareTo(((HoldingPK)pObject).customerID) == 0)
          return true;
          else
          return false;
          }

          }

          Then just specify it as you normally would in the
          deployment descriptor.

          <prim-key-class>
          StockTradingDataManagers.HoldingPK
          </prim-key-class>

          Hope that helps.

          Cheers,

          Noel.

          P.S. Sorry that the formatting went to shyte when
          I posted this...



          • 2. Re: Composite PK in CMP
            parshuram

            Hi,
            I tried with this one but when i call create method it gives transactionrollbackexception:null
            Also hoe can i map copmosite field in jaws.xml.
            Please Suggest me
            Thanks Parshuram

            • 3. Re: Composite PK in CMP
              nhebert

              Parshuram,

              Hmmm, can you send me a code snippet where the
              create(PK) is being called and the code for the
              PK class?

              Regarding jaws.xml, Composite key fields are mapped
              just like any other field. The significants of the
              fields is not implied in jaws.xml, but in the index
              created for the table in the physical datastore.

              Here is the snippet from the jaws.xml for the
              Holding bean I illustrated:

              ...

              <ejb-name>StockTrading/HoldingEntityBean</ejb-name>
              <table-name>Holdings</table-name>
              <create-table>False</create-table>
              <cmp-field>
              <field-name>customerID</field-name>
              <column-name>customer_ID</column-name>
              <jdbc-type>INTEGER</jdbc-type>
              <sql-type>INTEGER</sql-type>
              </cmp-field>
              <cmp-field>
              <field-name>name</field-name>
              <column-name>cust_name</column-name>
              <jdbc-type>VARCHAR</jdbc-type>
              <sql-type>VARCHAR(25)</sql-type>
              </cmp-field>
              <cmp-field>
              <field-name>price</field-name>
              <column-name>price</column-name>
              <jdbc-type>DOUBLE</jdbc-type>
              <sql-type>DOUBLE</sql-type>
              </cmp-field>
              <cmp-field>
              <field-name>quantity</field-name>
              <column-name>quantity</column-name>
              <jdbc-type>INTEGER</jdbc-type>
              <sql-type>INTEGER</sql-type>
              </cmp-field>

              ...

              Sorry for the lack of indents.

              Notice that CustomerID and name are regular fields
              in the jaws.xml. In my schema in the Holdings table
              I defined a unique composite key on
              (CustomerID,name) but you will not see that in the
              jaws.xml file.

              jaws.xml is used to precisely describe to the
              JAWS O-R mapper how the fields map to columns
              in a database table with the name, size, type, etc...

              Hope this helps.

              Cheers,

              Noel.
              nhebert@dingoblue.net.au



              • 4. Re: Composite PK in CMP
                mjlarsen

                Hello Parshuram & Noel (& anyone else),

                I am experiencing the same problem on 2.4.3 (with tomcat version) but am wondering if this is JDBC Driver-related? In this case the data-type in the table is (the non-ANSI) VARCHAR2. Ì've tried to unsuccessfully remap this in standardjaws.xml with:


                <java-type>java.lang.String</java-type>
                <jdbc-type>VARCHAR</jdbc-type>
                <sql-type>VARCHAR2(20)</sql-type>


                The error I get is (apologize in advance for the length):

                [Article23Report] TRANSACTION ROLLBACK EXCEPTION:Load failed; nested exception is:
                java.lang.NullPointerException; nested exception is:
                java.rmi.ServerException: Load failed; nested exception is:
                java.lang.NullPointerException
                [Article23Report] java.rmi.ServerException: Load failed; nested exception is:
                [Article23Report] java.lang.NullPointerException
                [Article23Report] at org.jboss.ejb.plugins.jaws.jdbc.JDBCLoadEntityCommand
                .execute(JDBCLoadEntityCommand.java:151)
                [Article23Report] at org.jboss.ejb.plugins.jaws.JAWSPersistenceManager.loa
                dEntity(JAWSPersistenceManager.java:156)
                [Article23Report] at org.jboss.ejb.plugins.CMPPersistenceManager.loadEntit
                y(CMPPersistenceManager.java:362)
                [Article23Report] at org.jboss.ejb.plugins.EntitySynchronizationIntercepto
                r.invoke(EntitySynchronizationInterceptor.java:286)
                [Article23Report] at org.jboss.ejb.plugins.EntityInstanceInterceptor.invok
                e(EntityInstanceInterceptor.java:208)
                [Article23Report] at org.jboss.ejb.plugins.EntityLockInterceptor.invoke(En
                tityLockInterceptor.java:136)
                [Article23Report] at org.jboss.ejb.plugins.TxInterceptorCMT.invokeNext(TxI
                nterceptorCMT.java:133)
                [Article23Report] at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransac
                tions(TxInterceptorCMT.java:307)
                [Article23Report] at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInter
                ceptorCMT.java:99)
                [Article23Report] at org.jboss.ejb.plugins.SecurityInterceptor.invoke(Secu
                rityInterceptor.java:128)
                [Article23Report] at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterc
                eptor.java:195)
                [Article23Report] at org.jboss.ejb.EntityContainer.invoke(EntityContainer.
                java:427)
                [Article23Report] at org.jboss.ejb.plugins.jrmp.server.JRMPContainerInvoke
                r.invoke(JRMPContainerInvoker.java:489)
                [Article23Report] at org.jboss.ejb.plugins.jrmp.interfaces.GenericProxy.in
                vokeContainer(GenericProxy.java:335)
                [Article23Report] at org.jboss.ejb.plugins.jrmp.interfaces.EntityProxy.inv
                oke(EntityProxy.java:133)
                [Article23Report] at $Proxy73.getRealizedGain(Unknown Source)
                [Article23Report] at ejb.report.ReportControllerBean.createArticle23Report
                (Unknown Source)
                [Article23Report] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native M
                ethod)
                [Article23Report] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown S
                ource)
                [Article23Report] at sun.reflect.InflatableMethodAccessorImpl.invoke(Unkno
                wn Source)
                [Article23Report] at java.lang.reflect.Method.invoke(Unknown Source)
                [Article23Report] at org.jboss.ejb.StatelessSessionContainer$ContainerInte
                rceptor.invoke(StatelessSessionContainer.java:543)
                [Article23Report] at org.jboss.ejb.plugins.StatelessSessionInstanceInterce
                ptor.invoke(StatelessSessionInstanceInterceptor.java:87)
                [Article23Report] at org.jboss.ejb.plugins.TxInterceptorCMT.invokeNext(TxI
                nterceptorCMT.java:133)
                [Article23Report] at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransac
                tions(TxInterceptorCMT.java:307)
                [Article23Report] at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInter
                ceptorCMT.java:99)
                [Article23Report] at org.jboss.ejb.plugins.SecurityInterceptor.invoke(Secu
                rityInterceptor.java:128)
                [Article23Report] at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterc
                eptor.java:195)
                [Article23Report] at org.jboss.ejb.StatelessSessionContainer.invoke(Statel
                essSessionContainer.java:286)
                [Article23Report] at org.jboss.ejb.plugins.jrmp.server.JRMPContainerInvoke
                r.invoke(JRMPContainerInvoker.java:395)
                [Article23Report] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native M
                ethod)
                [Article23Report] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown S
                ource)
                [Article23Report] at sun.reflect.InflatableMethodAccessorImpl.invoke(Unkno
                wn Source)
                [Article23Report] at java.lang.reflect.Method.invoke(Unknown Source)
                [Article23Report] at sun.rmi.server.UnicastServerRef.dispatch(Unknown Sour
                ce)
                [Article23Report] at sun.rmi.transport.Transport$1.run(Unknown Source)
                [Article23Report] at java.security.AccessController.doPrivileged(Native Me
                thod)
                [Article23Report] at sun.rmi.transport.Transport.serviceCall(Unknown Sourc
                e)
                [Article23Report] at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unk
                nown Source)
                [Article23Report] at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.
                run(Unknown Source)
                [Article23Report] at java.lang.Thread.run(Unknown Source)
                [Article23Report] Caused by: java.lang.NullPointerException
                [Article23Report] at org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand.getResult
                Object(JDBCCommand.java:504)
                [Article23Report] at org.jboss.ejb.plugins.jaws.jdbc.JDBCLoadEntityCommand
                .loadOneEntity(JDBCLoadEntityCommand.java:219)
                [Article23Report] at org.jboss.ejb.plugins.jaws.jdbc.JDBCLoadEntityCommand
                .handleResult(JDBCLoadEntityCommand.java:176)
                [Article23Report] at org.jboss.ejb.plugins.jaws.jdbc.JDBCQueryCommand.exec
                uteStatementAndHandleResult(JDBCQueryCommand.java:59)
                [Article23Report] at org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand.jdbcExecu
                te(JDBCCommand.java:160)
                [Article23Report] at org.jboss.ejb.plugins.jaws.jdbc.JDBCLoadEntityCommand
                .execute(JDBCLoadEntityCommand.java:147)
                [Article23Report] ... 40 more
                [Reporter] TRANSACTION ROLLBACK EXCEPTION:Load failed; nested exception is:
                java.lang.NullPointerException; nested exception is:
                java.rmi.ServerException: Load failed; nested exception is:
                java.lang.NullPointerException; nested exception is:
                javax.transaction.TransactionRolledbackException: Load failed; nested ex
                ception is:
                java.lang.NullPointerException; nested exception is:
                java.rmi.ServerException: Load failed; nested exception is:
                java.lang.NullPointerException
                [Reporter] javax.transaction.TransactionRolledbackException: Load failed; nested
                exception is:
                [Reporter] java.lang.NullPointerException; nested exception is:
                [Reporter] java.rmi.ServerException: Load failed; nested exception is:
                [Reporter] java.lang.NullPointerException
                [Reporter] at org.jboss.ejb.plugins.TxInterceptorCMT.invokeNext(TxIntercept
                orCMT.java:172)
                [Reporter] at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(Tx
                InterceptorCMT.java:307)
                [Reporter] at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCM
                T.java:99)
                [Reporter] at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInte
                rceptor.java:128)
                [Reporter] at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.ja
                va:195)
                [Reporter] at org.jboss.ejb.EntityContainer.invoke(EntityContainer.java:427
                )
                [Reporter] at org.jboss.ejb.plugins.jrmp.server.JRMPContainerInvoker.invoke
                (JRMPContainerInvoker.java:489)
                [Reporter] at org.jboss.ejb.plugins.jrmp.interfaces.GenericProxy.invokeCont
                ainer(GenericProxy.java:335)
                [Reporter] at org.jboss.ejb.plugins.jrmp.interfaces.EntityProxy.invoke(Enti
                tyProxy.java:133)
                [Reporter] at $Proxy73.getRealizedGain(Unknown Source)
                [Reporter] at ejb.report.ReportControllerBean.createArticle23Report(Unknown
                Source)
                [Reporter] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                [Reporter] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
                [Reporter] at sun.reflect.InflatableMethodAccessorImpl.invoke(Unknown Sourc
                e)
                [Reporter] at java.lang.reflect.Method.invoke(Unknown Source)
                [Reporter] at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.
                invoke(StatelessSessionContainer.java:543)
                [Reporter] at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.inv
                oke(StatelessSessionInstanceInterceptor.java:87)
                [Reporter] at org.jboss.ejb.plugins.TxInterceptorCMT.invokeNext(TxIntercept
                orCMT.java:133)
                [Reporter] at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(Tx
                InterceptorCMT.java:307)
                [Reporter] at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCM
                T.java:99)
                [Reporter] at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInte
                rceptor.java:128)
                [Reporter] at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.ja
                va:195)
                [Reporter] at org.jboss.ejb.StatelessSessionContainer.invoke(StatelessSessi
                onContainer.java:286)
                [Reporter] at org.jboss.ejb.plugins.jrmp.server.JRMPContainerInvoker.invoke
                (JRMPContainerInvoker.java:395)
                [Reporter] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                [Reporter] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
                [Reporter] at sun.reflect.InflatableMethodAccessorImpl.invoke(Unknown Sourc
                e)
                [Reporter] at java.lang.reflect.Method.invoke(Unknown Source)
                [Reporter] at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
                [Reporter] at sun.rmi.transport.Transport$1.run(Unknown Source)
                [Reporter] at java.security.AccessController.doPrivileged(Native Method)
                [Reporter] at sun.rmi.transport.Transport.serviceCall(Unknown Source)
                [Reporter] at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Sou
                rce)
                [Reporter] at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unkn
                own Source)
                [Reporter] at java.lang.Thread.run(Unknown Source)
                [Reporter] Caused by: java.rmi.ServerException: Load failed; nested exception is
                :
                [Reporter] java.lang.NullPointerException
                [Reporter] at org.jboss.ejb.plugins.jaws.jdbc.JDBCLoadEntityCommand.execute
                (JDBCLoadEntityCommand.java:151)
                [Reporter] at org.jboss.ejb.plugins.jaws.JAWSPersistenceManager.loadEntity(
                JAWSPersistenceManager.java:156)
                [Reporter] at org.jboss.ejb.plugins.CMPPersistenceManager.loadEntity(CMPPer
                sistenceManager.java:362)
                [Reporter] at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invoke
                (EntitySynchronizationInterceptor.java:286)
                [Reporter] at org.jboss.ejb.plugins.EntityInstanceInterceptor.invoke(Entity
                InstanceInterceptor.java:208)
                [Reporter] at org.jboss.ejb.plugins.EntityLockInterceptor.invoke(EntityLock
                Interceptor.java:136)
                [Reporter] at org.jboss.ejb.plugins.TxInterceptorCMT.invokeNext(TxIntercept
                orCMT.java:133)
                [Reporter] ... 34 more
                [Reporter] Caused by: java.lang.NullPointerException
                [Reporter] at org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand.getResultObject(J
                DBCCommand.java:504)
                [Reporter] at org.jboss.ejb.plugins.jaws.jdbc.JDBCLoadEntityCommand.loadOne
                Entity(JDBCLoadEntityCommand.java:219)
                [Reporter] at org.jboss.ejb.plugins.jaws.jdbc.JDBCLoadEntityCommand.handleR
                esult(JDBCLoadEntityCommand.java:176)
                [Reporter] at org.jboss.ejb.plugins.jaws.jdbc.JDBCQueryCommand.executeState
                mentAndHandleResult(JDBCQueryCommand.java:59)
                [Reporter] at org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand.jdbcExecute(JDBCC
                ommand.java:160)
                [Reporter] at org.jboss.ejb.plugins.jaws.jdbc.JDBCLoadEntityCommand.execute
                (JDBCLoadEntityCommand.java:147)
                [Reporter] ... 40 more

                Here is my jaws.xml:


                java:/OracleDB
                <type-mapping>Oracle8</type-mapping>

                <enterprise-beans>

                <ejb-name>Article23Report</ejb-name>
                <table-name>transaktion</table-name>
                <remove-table>false</remove-table>
                <cmp-field>

                <field-name>clientId</field-name>
                <column-name>GLAEUBIGER_ID</column-name>
                <jdbc-type>VARCHAR2</jdbc-type>
                <sql-type>VARCHAR2</sql-type>
                </cmp-field>
                <cmp-field>
                <field-name>businessGroupId</field-name>
                <column-name>WG_ID</column-name>
                <jdbc-type>VARCHAR2</jdbc-type>
                <sql-type>VARCHAR2</sql-type>
                </cmp-field>
                <cmp-field>
                <field-name>realizedGain</field-name>
                <column-name>VERAUESSERUNGSGEWINN</column-name>
                </cmp-field>

                findByClientId
                GLAEUBIGER_ID = {0}
                GLAEUBIGER_ID
                <read-ahead>true</read-ahead>


                </enterprise-beans>


                Ânyone got any ideas?

                Cheers!
                Mike