1 Reply Latest reply on May 21, 2003 10:07 AM by mighty_joe

    Need Help With CMP Example

    mighty_joe

      I'm working my way through Mastering Enterprise JavaBeans by Edi Roman and am having a problem with the CMP example in Chapter 7. The bean deploys fine, but when I run the client, JBoss coughs up the following error:

      09:53:53,760 INFO [STDOUT] ejbCreate
      09:53:53,841 ERROR [Product] Error checking if entity exists
      java.sql.SQLException: Unexpected token: in statement [SELECT COUNT(*) FROM PRODUCT WHERE ]

      when it appears to be checking that the database table exists (I'm using the Hypersonic database). Now, one of the problems is that the database table I'm using is actually named ProductTable, so at the very least, my deployment descriptors are not quite 100%

      ------------------- text of ejb-jar.xml follows --------------------------
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE ejb-jar PUBLIC
      "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
      "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
      <ejb-jar>
      <enterprise-beans>

      <ejb-name>Product</ejb-name>
      examples.ProductHome
      examples.Product
      <local-home>examples.ProductLocalHome</local-home>
      examples.ProductLocal
      <ejb-class>examples.ProductBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      <prim-key-field>productID</prim-key-field>
      False

      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>ProductBean</abstract-schema-name>

      <cmp-field>
      <field-name>productID</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>name</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>description</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>basePrice</field-name>
      </cmp-field>


      <query-method>
      <method-name>findByName</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>
      </query-method>
      <ejb-ql>
      <![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE a.name = ?1]]>
      </ejb-ql>



      <query-method>
      <method-name>findByDescription</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>
      </query-method>
      <ejb-ql>
      <![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE a.description = ?1]]>
      </ejb-ql>



      <query-method>
      <method-name>findByBasePrice</method-name>
      <method-params>
      <method-param>double</method-param>
      </method-params>
      </query-method>
      <ejb-ql>
      <![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE a.basePrice = ?1]]>
      </ejb-ql>



      <query-method>
      <method-name>findExpensiveProducts</method-name>
      <method-params>
      <method-param>double</method-param>
      </method-params>
      </query-method>
      <ejb-ql>
      <![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE a.basePrice > ?1]]>
      </ejb-ql>



      <query-method>
      <method-name>findCheapProducts</method-name>
      <method-params>
      <method-param>double</method-param>
      </method-params>
      </query-method>
      <ejb-ql>
      <![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE a.basePrice < ?1]]>
      </ejb-ql>



      <query-method>
      <method-name>findAllProducts</method-name>
      <method-params>
      </method-params>
      </query-method>
      <ejb-ql>
      <![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE a.productID IS NOT NULL]]>
      </ejb-ql>



      </enterprise-beans>

      <assembly-descriptor>
      <container-transaction>

      <ejb-name>Product</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>*</method-name>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>
      </ejb-jar>

      ------------------------- text of jbosscmp-jdbc.xml follows --------------

      <jbosscmp-jdbc>


      java:/DefaultDS
      <datasource-mapping>Hypersonic SQL</datasource-mapping>
      <pk-constraint>false</pk-constraint>



      <ejb-name>Product</ejb-name>
      <table-name>ProductTable</table-name>
      <cmp-field>
      <field-name>productID</field-name>
      <column-name>id</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>name</field-name>
      <column-name>name</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>description</field-name>
      <column-name>description</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>basePrice</field-name>
      <column-name>basePrice</column-name>
      </cmp-field>


      </jbosscmp-jdbc>

      One further question: The examples in this book for Chapter 7 are noted in the errata as being faulty as far as some of the methods use java.lang.String for a primary key and others use examples.ProductPK as a value for the primary key. The errata says to correct the methods to use one or the other. I can get the bean to deploy using String as the primary key, but not ProductPK. Anyone get ProductPK to work?

        • 1. Re: Need Help With CMP Example
          mighty_joe

          Answer to my own question:
          The field:
          <prim-key-field>productID</prim-key-field>
          in ejb-jar.xml should be:
          <primkey-field>productID</primkey-field>

          The error is caused because the primary field name was not specified, so it is absent in the SQL call.