4 Replies Latest reply on Mar 6, 2002 3:24 PM by deathstar

    FindByForeignKey failed

    hubs2121

      Hello,
      I tried a simple CMR example using two tables as folows:

      parent --> parentid, name
      child --> childid, name, parentid

      where parentid in the child table is the Foreign Key to the parent table.
      The problem occurs when I try to call the cmr accessor method getChilds( ), I get the following error:

      The Parent is:
      1 John Smith
      The Children are:
      java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
      java.rmi.ServerException: FindByForeignKey failed
      Embedded Exception
      Column not found: Unknown column 'parent_parentid' in 'where clause'; nested exception is:
      javax.ejb.EJBException: FindByForeignKey failed
      Embedded Exception
      Column not found: Unknown column 'parent_parentid' in 'where clause'


      Why is it calling the foreign key field "parent_parentid" when it should be just "parentid"? Here is my jbosscmp-jdbc.xml:

      <jbosscmp-jdbc>

      java:/mySQLDS
      <datasource-mapping>mySQL</datasource-mapping>


      <enterprise-beans>


      <ejb-name>Child</ejb-name>
      <table-name>child</table-name>

      <cmp-field>
      <field-name>childid</field-name>
      <column-name>childid</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>name</field-name>
      <column-name>name</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>parentid</field-name>
      <column-name>parentid</column-name>
      </cmp-field>




      <ejb-name>Parent</ejb-name>
      <table-name>parent</table-name>

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



      </enterprise-beans>


      <ejb-relation>
      <ejb-relation-name>parent-child</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>parent-has-child</ejb-relationship-role-name>
      <fk-constraint>true</fk-constraint>
      <key-fields/>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>child-has-parent</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>parentid</field-name>
      <column-name>parentid</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>
      </ejb-relation>


      </jbosscmp-jdbc>


      When I change the column name in my database to parent_parentid it works fine, but the mapping in jbosscmp-jdbc.xml should take care of that and allow me to call it whatever I want, no?

      Any ideas?

      Thanks,

      Steve

        • 1. Re: FindByForeignKey failed
          deathstar

          Iam having the same problem!!!! Does anyone have a solution???

          • 2. Re: FindByForeignKey failed
            hubs2121

            I never actually solved this problem...instead I am now using MVCSoft's persistence manager, and the same code works fine.

            Steve

            • 3. Re: FindByForeignKey failed
              dsundstrom

              You have the keys backwards.

              When defining keys you are always providing the mapping for the primary key fields of the current entity. If the mapping happens to be a foreign key mapping the columns will be added to the related entity's table.

              • 4. Re: FindByForeignKey failed
                deathstar

                Fixed the problem. parentid is a foreignkey in your child table.Just try changing your code to the below:


                <ejb-relation>
                <ejb-relation-name>parent-child</ejb-relation-name>
                <foreign-key-mapping>
                <ejb-relationship-role>
                <ejb-relationship-role-name>parent-has-child</ejb-relationship-role-name>
                <foreign-key-fields/>
                </ejb-relationship-role>
                <ejb-relationship-role>
                <ejb-relationship-role-name>child-has-parent</ejb-relationship-role-name>
                <foreign-key-fields>
                <foreign-key-field>
                <field-name>parentid</field-name>
                <column-name>parentid</column-name>
                </foreign-key-field>
                </foreign-key-fields>
                </ejb-relationship-role>
                </foreign-key-mapping>
                </ejb-relation>