2 Replies Latest reply on Jul 4, 2002 11:55 AM by Dain Sundstrom

    how mapping a one-many relationship

    ygwangking Newbie

      I use jboss3.0 final and mysql and I created two beans bbs_root_context and bbs_children_conext;
      they are descripted in ejb-jar.xml:
      <ejb-jar>
      <enterprise-beans>

      <ejb-name>bbs_root_context</ejb-name>
      ....
      <abstract-schema-name>bbs_root_context</abstract-schema-name>
      <cmp-field>
      <field-name>root_id</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>user_id</field-name>
      </cmp-field>
      ....



      <ejb-name>bbs_children_context</ejb-name>
      ......
      <abstract-schema-name>bbs_children_context</abstract-schema-name>
      <cmp-field>
      <field-name>child_id</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>root_id</field-name>
      </cmp-field>
      ......

      </enterprise-beans>
      ......

      <ejb-relation>
      <ejb-relation-name>root-children</ejb-relation-name>
      <ejb-relationship-role>

      <ejb-relationship-role-name>root-has-children</ejb-relationship-role-name>
      One
      <relationship-role-source>
      <ejb-name>bbs_root_context</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>children</cmr-field-name>
      <cmr-field-type>java.util.Collection</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>

      <ejb-relationship-role-name>children-has-root</ejb-relationship-role-name>
      Many
      <cascade-delete/>
      <relationship-role-source>
      <ejb-name>bbs_children_context</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>root</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      </ejb-relation>

      </ejb-jar>
      here,root_id is bbs_root_context's primary key;
      In jbosscmp-jdbc.xml
      ......

      <ejb-relation>
      <ejb-relation-name>root-children</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>root-has-children</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>root_id</field-name>
      <column-name>root_id</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>children-has-root</ejb-relationship-role-name>
      <key-fields/>
      </ejb-relationship-role>
      </ejb-relation>

      ....
      when I run my test grogram to try to create bbs_root_context bean,I got a exception:
      java.rmi.ServerException: RemoteException occurred in server thread; nested exce
      ption is:
      java.rmi.ServerException: null
      Embedded Exception
      Could not create entity:java.sql.SQLException: General error: Column 'root_id' s
      pecified twice; nested exception is:
      javax.ejb.EJBException: null
      Embedded Exception
      Could not create entity:java.sql.SQLException: General error: Column 'root_id' s
      pecified twice
      at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:292)
      at sun.rmi.transport.Transport$1.run(Transport.java:148)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
      at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:4
      60)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
      .java:701)
      at java.lang.Thread.run(Thread.java:536)
      at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Stream
      RemoteCall.java:247)
      at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:
      223)
      at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:133)
      at org.jboss.invocation.jrmp.server.JRMPInvoker_Stub.invoke(Unknown Sour
      ce)
      at org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy.invoke(JRMPInvo
      kerProxy.java:128)
      at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.jav
      a:108)
      at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.
      java:73)
      at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:7
      6)
      at org.jboss.proxy.ejb.StatefulSessionInterceptor.invoke(StatefulSession
      Interceptor.java:117)
      at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:76)
      at $Proxy1.postChildrenConext(Unknown Source)
      at Test.Test.runExample(Unknown Source)
      at Test.Test.main(Unknown Source)
      Caused by: java.rmi.ServerException: null
      Embedded Exception
      Could not create entity:java.sql.SQLException: General error: Column 'root_id' s
      pecified twice; nested exception is:
      javax.ejb.EJBException: null


      Can any one help me?thank you.

        • 1. Re: how mapping a one-many relationship
          JinWang JinWang Newbie

          I wish you already fixed your problem so far. just in case
          it is still a problem you can simply fix it by change the following :
          <field-name>root_id</field-name>
          <column-name>root_id</column-name>

          into :

          <field-name>root_id</field-name>
          <column-name>root</column-name>

          in the relationship mapping for relation "root-children"
          in your jbosscmp-jdbc.xml file.

          The reason is that in your bbs_children_context table JBoss tries to create a new column for
          your CMR field with the name given in the <column-name> tag . But you already defined a CMP field "root_id" in the table. You can simply give a different name from all
          the column's names for the CMR field (foreign key).

          good luck !

          • 2. Re: how mapping a one-many relationship
            Dain Sundstrom Master

            You are not allowed to have a CMP and CMR field map to the same column.