8 Replies Latest reply on Apr 27, 2004 1:56 PM by roger_cmu

    Simplest one-many relationship: not working

    roger_cmu Newbie

      I am new to ejb-relationship but , believe me, I tried hard. Here is the code: A customer can have many addresses.

      AdderssBean.java
      // CMP field accessors

      public abstract String getStreet();
      public abstract void setStreet(String street);

      public abstract Integer getId();
      public abstract void setId(Integer id);

      CustomerBean.java
      // CMP field accessors

      public abstract String getLastname();
      public abstract void setLastname(String lastname);
      public abstract Integer getId();
      public abstract void setId(Integer id);
      // CMR field accessors
      public abstract void setAddress(Collection col);
      public abstract Collection getAddress();

      ejb-jar.xml

      <display-name>CustomerEJB</display-name>
      <ejb-name>CustomerEJB</ejb-name>
      <local-home>test.ejb.com.sbm.cts.customerHome</local-home>
      test.ejb.com.sbm.cts.Customer
      <ejb-class>test.ejb.com.sbm.cts.CustomerBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Integer</prim-key-class>
      False
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>CustomerSchema</abstract-schema-name>
      <cmp-field><field-name>lastname</field-name></cmp-field> <cmp-field><field-name>id</field-name></cmp-field>
      <primkey-field>id</primkey-field>
      <ejb-local-ref>
      <ejb-ref-name>ejb/address</ejb-ref-name>
      <ejb-ref-type>Entity</ejb-ref-type>
      <local-home>test.ejb.com.sbm.cts.AddressHome</local-home>
      test.ejb.com.sbm.cts.Address
      <ejb-link>AddressEJB</ejb-link>
      </ejb-local-ref>
      <resource-ref>
      <res-ref-name>jdbc/BankDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
      <res-sharing-scope>Shareable</res-sharing-scope>
      </resource-ref>


      <display-name>AddressEJB</display-name>
      <ejb-name>AddressEJB</ejb-name>
      <local-home>test.ejb.com.sbm.cts.AddressHome</local-home>
      test.ejb.com.sbm.cts.Address
      <ejb-class>test.ejb.com.sbm.cts.AddressBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Integer</prim-key-class>
      False
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>AddressSchema</abstract-schema-name>
      <cmp-field><field-name>street</field-name></cmp-field>
      <cmp-field><field-name>id</field-name></cmp-field>
      <primkey-field>id</primkey-field>
      <resource-ref>
      <res-ref-name>jdbc/BankDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
      <res-sharing-scope>Shareable</res-sharing-scope>
      </resource-ref>


      jbosscmp-jdbc.xml

      <ejb-name>CustomerEJB</ejb-name>
      <table-name>customer</table-name>
      <create-table>false</create-table>
      <remove-table>false</remove-table>

      <cmp-field>
      <field-name>lastname</field-name>
      <column-name>lastname</column-name>
      </cmp-field>

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



      <ejb-name>AddressEJB</ejb-name>
      <table-name>address</table-name>
      <create-table>false</create-table>
      <remove-table>false</remove-table>
      <cmp-field>
      <field-name>street</field-name>
      <column-name>street</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>id</field-name>
      <column-name>addressid</column-name>
      </cmp-field>


      <ejb-relation>
      <ejb-relation-name>Customer-address</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>Customer-has-an-address</ejb-relationship-role-name>
      <key-fields>
      <field-name>address</field-name>
      <column-name>custid</column-name>
      </key-fields>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>an-address-belongs-to-a-customer</ejb-relationship-role-name>
      <key-fields/>
      </ejb-relationship-role>
      </ejb-relation>



      tables:
      CREATE TABLE customer
      (
      lastname VARCHAR(10),
      custid int(4) auto_increment,
      CONSTRAINT pk_customer PRIMARY KEY (custid)
      );



      CREATE TABLE address
      (
      street VARCHAR(10),
      addressid int(4) auto_increment,
      custid int(4),
      CONSTRAINT pk_user PRIMARY KEY (addressid)
      );

      Suggestions are highly appreciated.

        • 1. Re: Simplest one-many relationship: not working
          Alexey Loubyansky Master

          Relationships should also be configured in ejb-jar.xml, not only its mapping in jbosscmp-jdbc.xml

          • 2. Re: Simplest one-many relationship: not working
            roger_cmu Newbie

            Sorry. The relationship on ejb-jar.xml looks like this.


            <ejb-relation>
            <ejb-relation-name>
            Customer-address
            </ejb-relation-name>
            <ejb-relationship-role>
            <ejb-relationship-role-name>
            Customer-has-an-address
            </ejb-relationship-role-name>
            One
            <relationship-role-source>
            <ejb-name>CustomerEJB</ejb-name>
            </relationship-role-source>
            <cmr-field>
            <cmr-field-name>address</cmr-field-name>
            <cmr-field-type>java.util.Collection</cmr-field-type>
            </cmr-field>
            </ejb-relationship-role>
            <ejb-relationship-role>
            <ejb-relationship-role-name>
            an-address-belongs-to-a-customer
            </ejb-relationship-role-name>
            Many
            <relationship-role-source>
            <ejb-name>AddressEJB</ejb-name>
            </relationship-role-source>
            </ejb-relationship-role>
            </ejb-relation>

            • 3. Re: Simplest one-many relationship: not working
              Alexey Loubyansky Master

              In key-fields mapping you map primary key fields to foreign key columns. So, there should be 'id' instead of 'address'.
              BTW, you didn't post niether exceptioin nor some description proving it does not work.

              • 4. Re: Simplest one-many relationship: not working
                roger_cmu Newbie

                Thanks a lot loubyansky. I incorporate your suggestion. Now the error is:


                17:15:52,549 ERROR [EntityContainer] Starting failed
                org.jboss.deployment.DeploymentException: Atleast one role of a foreign-key mapped relationship must have key fields (or <primkey-field> is missing from ejb-jar.xml): ejb-relation-name=Customer-address
                at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCRelationMetaData.(JDBCRelationMetaData.java:347)
                at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCApplicationMetaData.(JDBCApplicationMetaData.java:470)
                at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCXmlFileLoader.load(JDBCXmlFileLoader.java:75)
                at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.loadJDBCEntityMetaData(JDBCStoreManager.java:706)
                at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.initStoreManager(JDBCStoreManager.java:409)
                at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.start(JDBCStoreManager.java:357)
                at org.jboss.ejb.plugins.CMPPersistenceManager.start(CMPPersistenceManager.java:152)
                at org.jboss.ejb.EntityContainer.startService(EntityContainer.java:342)
                at org.jboss.system.ServiceMBeanSupport.start(ServiceMBeanSupport.java:192)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:324)
                at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
                at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:546)
                at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:976)
                at $Proxy14.start(Unknown Source)
                at org.jboss.system.ServiceController.start(ServiceController.java:394)
                at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:324)
                at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
                at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:546)
                at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:177)
                at $Proxy42.start(Unknown Source)
                at org.jboss.ejb.EjbModule.startService(EjbModule.java:331)
                at org.jboss.system.ServiceMBeanSupport.start(ServiceMBeanSupport.java:192)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:324)
                at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
                at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:546)
                at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:976)
                at $Proxy14.start(Unknown Source)
                at org.jboss.system.ServiceController.start(ServiceController.java:394)
                at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:324)
                at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
                at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:546)
                at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:177)
                at $Proxy12.start(Unknown Source)
                at org.jboss.ejb.EJBDeployer.start(EJBDeployer.java:544)
                at org.jboss.deployment.MainDeployer.start(MainDeployer.java:832)
                at org.jboss.deployment.MainDeployer.start(MainDeployer.java:824)
                at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:642)
                at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:605)
                at sun.reflect.GeneratedMethodAccessor18.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:324)
                at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
                at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:546)
                at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:177)
                at $Proxy6.deploy(Unknown Source)
                at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymentScanner.java:302)
                at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentScanner.java:458)
                at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.doScan(AbstractDeploymentScanner.java:201)
                at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.loop(AbstractDeploymentScanner.java:212)
                at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.run(AbstractDeploymentScanner.java:191)

                • 5. Re: Simplest one-many relationship: not working
                  Alexey Loubyansky Master

                  Try this way:

                  <key-fields>
                   <key-field>
                   <field-name>id</field-name>
                   <column-name>custid</column-name>
                   </key-field>
                  </key-fields>
                  


                  • 6. Re: Simplest one-many relationship: not working
                    roger_cmu Newbie

                    I'm sorry but I didn't get it. How is this solution different from replacing 'address' by 'id' -- suggested earlier. As mentioned, I replaced 'address' by 'id' and then I got the error posted in the previous message.

                    thanks.
                    - Roger

                    • 7. Re: Simplest one-many relationship: not working
                      Alexey Loubyansky Master

                      You were missing the key-field tag.

                      • 8. Re: Simplest one-many relationship: not working
                        roger_cmu Newbie

                        Thanks a lot loubyansky. You are highly appreciated. I owe you one.

                        - Roger