You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@geronimo.apache.org by Dale de los Reyes <dr...@hotmail.com> on 2006/03/06 22:18:08 UTC

Request for CMP using Compound Key example

Hello,

If anyone knows an example CMP entity bean that uses a compound/primary key 
class, please point me in the right direction. Or if someone knows what I'm 
missing. I would appreciate the help.

Using Geronimo with Tomcat 1.0 and Derby bundled in same package:

I created a simple entity bean with the following methods in the home class:
(Note: full syntax omiited for brevity)
someBean create(SomePrimaryKey key)
someBean findByPrimaryKey(SomePrimaryKey key)
Collection findByAll()

public final class SomePrimaryKey
{
   public Integer id;
   public String desc;

   SomePrimaryKey() {}

   public boolean equals(Object other) {
      if (other instanceof SomePrimaryKey) {
         return (id.equals(((SomePrimaryKey)other).id));
      }
   }

   public int hashCode()
   {
      return id.hashCode();
   } // end hashCode
}

I know the above isn't using a compound key "per se", but GERONIMO-1361 
regarding compound keys with only 1 key is now closed, and should be working 
in Geronimo 1.0.

Anyway, I'm able to use this key class with BMP. However, when I switch the 
implementation to CMP, only the "create" and "findByAll" methods work. The 
"findByPrimaryKey" is unable to locate an existing entry in the database and 
instead returns an "ObjectNotFoundException".

When I convert the CMP to use a simple primary key (i.e. Integer 
primaryKey), all the CMP home methods work.

The examples I've seen so far (Product Line example in Mastering EJB 2nd ed 
and the examples bundled in the Geronimo docs), do not specify any EJB-QL 
code in the descriptors for the "findByPrimaryKey" method.

Here's a portion of my ejb-jar.xml descriptor
   <enterprise-beans>
      <entity>
         <ejb-name>SomeCMPEntityBean</ejb-name>
         <home>some.ejb.SomeCMPHome</home>
         <remote>some.ejb.SomeCMP</remote>
         <ejb-class>some.ejb.SomeCMPEntityBean</ejb-class>

         <persistence-type>Container</persistence-type>
         <!-- <prim-key-class>java.lang.Integer</prim-key-class> -->
         <prim-key-class>some.ejb.SomePrimaryKey</prim-key-class>
         <reentrant>False</reentrant>
         <cmp-version>2.x</cmp-version>
         <abstract-schema-name>SomeCMP</abstract-schema-name>

         <cmp-field>
            <field-name>id</field-name>
         </cmp-field>

         <cmp-field>
            <field-name>desc</field-name>
         </cmp-field>

         <!-- omitted when using compound key -->
         <!-- <primkey-field>id</primkey-field> -->

         <resource-ref>
            <description>DataSource for MachineShop Database</description>
            <res-ref-name>jdbc/MachineShopDataSource</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
         </resource-ref>

         <!-- No query tag needed for findByPrimaryKey? -->

         <query>
            <query-method>
               <method-name>findByAll</method-name>
               <method-params></method-params>
            </query-method>

            <ejb-ql>
            <![CDATA[select object(b) from SomeCMP as b]]>
            </ejb-ql>
         </query>
      </entity>
   <enterprise-beans>

Here's a portion of my openejb-jar.xml
<cmp-connection-factory>
   <resource-link>SomeDatabasePool</resource-link>
</cmp-connection-factory>

<enterprise-beans>
    <entity>
        <ejb-name>SomeCMPEntityBean</ejb-name>
        <jndi-name>SomeCMPRemoteEntity</jndi-name>

        <table-name>generic_table</table-name>
        <cmp-field-mapping>
           <cmp-field-name>id</cmp-field-name>
           <table-column>id</table-column>
        </cmp-field-mapping>

        <cmp-field-mapping>
           <cmp-field-name>desc</cmp-field-name>
           <table-column>description</table-column>
        </cmp-field-mapping>

        <resource-ref>
            <ref-name>jdbc/SomeDataSource</ref-name>
            <resource-link>SomeDatabasePool</resource-link>
        </resource-ref>
    </entity>
</enterprise-beans>

Again, any help is most appreciated.

Thanks,
Dale



RE: Request for CMP using Compound Key example

Posted by Dale de los Reyes <dr...@hotmail.com>.
Ugh, my bad. I need to make a correction on the ejb home methods. They 
should be:

SomeCMP create(SomePrimaryKey key)
SomeCMP findByPrimaryKey(SomePrimaryKey key)

NOT

>someBean create(SomePrimaryKey key)
>someBean findByPrimaryKey(SomePrimaryKey key)

Apologies,
Dale


>From: "Dale de los Reyes" <dr...@hotmail.com>
>Reply-To: user@geronimo.apache.org
>To: user@geronimo.apache.org
>Subject: Request for CMP using Compound Key example
>Date: Mon, 06 Mar 2006 13:18:08 -0800
>
>Hello,
>
>If anyone knows an example CMP entity bean that uses a compound/primary key 
>class, please point me in the right direction. Or if someone knows what I'm 
>missing. I would appreciate the help.
>
>Using Geronimo with Tomcat 1.0 and Derby bundled in same package:
>
>I created a simple entity bean with the following methods in the home 
>class:
>(Note: full syntax omiited for brevity)
>someBean create(SomePrimaryKey key)
>someBean findByPrimaryKey(SomePrimaryKey key)
>Collection findByAll()
>
>public final class SomePrimaryKey
>{
>   public Integer id;
>   public String desc;
>
>   SomePrimaryKey() {}
>
>   public boolean equals(Object other) {
>      if (other instanceof SomePrimaryKey) {
>         return (id.equals(((SomePrimaryKey)other).id));
>      }
>   }
>
>   public int hashCode()
>   {
>      return id.hashCode();
>   } // end hashCode
>}
>
>I know the above isn't using a compound key "per se", but GERONIMO-1361 
>regarding compound keys with only 1 key is now closed, and should be 
>working in Geronimo 1.0.
>
>Anyway, I'm able to use this key class with BMP. However, when I switch the 
>implementation to CMP, only the "create" and "findByAll" methods work. The 
>"findByPrimaryKey" is unable to locate an existing entry in the database 
>and instead returns an "ObjectNotFoundException".
>
>When I convert the CMP to use a simple primary key (i.e. Integer 
>primaryKey), all the CMP home methods work.
>
>The examples I've seen so far (Product Line example in Mastering EJB 2nd ed 
>and the examples bundled in the Geronimo docs), do not specify any EJB-QL 
>code in the descriptors for the "findByPrimaryKey" method.
>
>Here's a portion of my ejb-jar.xml descriptor
>   <enterprise-beans>
>      <entity>
>         <ejb-name>SomeCMPEntityBean</ejb-name>
>         <home>some.ejb.SomeCMPHome</home>
>         <remote>some.ejb.SomeCMP</remote>
>         <ejb-class>some.ejb.SomeCMPEntityBean</ejb-class>
>
>         <persistence-type>Container</persistence-type>
>         <!-- <prim-key-class>java.lang.Integer</prim-key-class> -->
>         <prim-key-class>some.ejb.SomePrimaryKey</prim-key-class>
>         <reentrant>False</reentrant>
>         <cmp-version>2.x</cmp-version>
>         <abstract-schema-name>SomeCMP</abstract-schema-name>
>
>         <cmp-field>
>            <field-name>id</field-name>
>         </cmp-field>
>
>         <cmp-field>
>            <field-name>desc</field-name>
>         </cmp-field>
>
>         <!-- omitted when using compound key -->
>         <!-- <primkey-field>id</primkey-field> -->
>
>         <resource-ref>
>            <description>DataSource for MachineShop Database</description>
>            <res-ref-name>jdbc/MachineShopDataSource</res-ref-name>
>            <res-type>javax.sql.DataSource</res-type>
>            <res-auth>Container</res-auth>
>         </resource-ref>
>
>         <!-- No query tag needed for findByPrimaryKey? -->
>
>         <query>
>            <query-method>
>               <method-name>findByAll</method-name>
>               <method-params></method-params>
>            </query-method>
>
>            <ejb-ql>
>            <![CDATA[select object(b) from SomeCMP as b]]>
>            </ejb-ql>
>         </query>
>      </entity>
>   <enterprise-beans>
>
>Here's a portion of my openejb-jar.xml
><cmp-connection-factory>
>   <resource-link>SomeDatabasePool</resource-link>
></cmp-connection-factory>
>
><enterprise-beans>
>    <entity>
>        <ejb-name>SomeCMPEntityBean</ejb-name>
>        <jndi-name>SomeCMPRemoteEntity</jndi-name>
>
>        <table-name>generic_table</table-name>
>        <cmp-field-mapping>
>           <cmp-field-name>id</cmp-field-name>
>           <table-column>id</table-column>
>        </cmp-field-mapping>
>
>        <cmp-field-mapping>
>           <cmp-field-name>desc</cmp-field-name>
>           <table-column>description</table-column>
>        </cmp-field-mapping>
>
>        <resource-ref>
>            <ref-name>jdbc/SomeDataSource</ref-name>
>            <resource-link>SomeDatabasePool</resource-link>
>        </resource-ref>
>    </entity>
></enterprise-beans>
>
>Again, any help is most appreciated.
>
>Thanks,
>Dale
>
>