13 Replies Latest reply on Nov 25, 2002 11:33 AM by Henrik Andersson

    As always, relationships problems

    Harald Newbie

      I am developing a enterprise server application, manly by taking availibal shareware components and adjust them for my need.
      The strategy worked just fine.... until I run across the CMR and the relationships that need to be defined in the jbosscmp-jdbc file.
      I should add that the application works on the RI, so my problem is JBoss config specific.
      I am stuck on the following problem:

      I have defined the rel. in the jbosscmp-jdbc.xml as following:
      <jbosscmp-jdbc>

      java:/MySqlDS
      <datasource-mapping>mySQL</datasource-mapping>
      <create-table>true</create-table>
      <remove-table>true</remove-table>
      <read-only>false</read-only>
      <time-out>300</time-out>
      <pk-constraint>true</pk-constraint>
      <fk-constraint>false</fk-constraint>
      <row-locking>false</row-locking>
      <preferred-relation-mapping>foreign-key</preferred-relation-mapping>

      <enterprise-beans>


      <ejb-name>MyContactEJB</ejb-name>
      <jndi-name>MyContactEJB</jndi-name>
      <ior-security-config>
      <transport-config>
      supported
      supported
      <establish-trust-in-target>supported</establish-trust-in-target>
      <establish-trust-in-client>supported</establish-trust-in-client>
      </transport-config>
      <as-context>
      <auth-method></auth-method>


      </as-context>
      <sas-context>
      <caller-propagation>supported</caller-propagation>
      </sas-context>
      </ior-security-config>
      <ejb-ref>
      <ejb-ref-name>AddressEJB</ejb-ref-name>
      <jndi-name>AddressEJB</jndi-name>
      </ejb-ref>
      <gen-classes />
      <ejb20-cmp>
      <sql-statement>
      .
      .
      .
      .
      </ejb20-cmp>

      .
      .
      .
      .

      <ejb-relation>
      <ejb-relation-name>MyAdd</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>MyContactEJB</ejb-relationship-role-name>
      <key-fields>
      <key-field>address</key-field>
      </key-fields>
      <read-ahead>on-load</read-ahead>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>AddressEJB</ejb-relationship-role-name>
      <key-fields/>
      <read-ahead>on-load</read-ahead>
      </ejb-relationship-role>
      </ejb-relation>


      An in the ejb-jar.xml:

      <ejb-relation>
      <ejb-relation-name>MyAdd</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>MyContactEJB</ejb-relationship-role-name>
      One
      <relationship-role-source>
      <ejb-name>MyContactEJB</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>address</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>AddressEJB</ejb-relationship-role-name>
      One
      <cascade-delete />
      <relationship-role-source>
      <ejb-name>AddressEJB</ejb-name>
      </relationship-role-source>
      </ejb-relationship-role>
      </ejb-relation>


      I have spent a week on this now without making any progress, it seems like Jboss ignor all my efforts in this eara and only
      replay this warning:
      2002-11-20 22:51:10,743 WARN [org.jboss.system.ServiceController] Problem starting service jboss.j2ee:jndiName=local/MyContactEJB,service=EJB
      org.jboss.deployment.DeploymentException: Atleast one role of a foreign-key mapped relationship must have key fields: ejb-relation-name=MyAdd.

      For the cases where I do not have any relationships between the EJBs everything works fine and the tables are created, so it is only on this..relationships!!!

      If anyone has any light to shed on this....pleace

        • 1. Re: As always, relationships problems
          Tim Newbie


          I can't quite tell from all the xml in your posting but I got this error message when I had key fields missing from at least one end of the relationship mapping definition in the jbosscmp-jdbc.xml file.

          Consider the following extract from a relationship mapping definition in jbosscmp-jdbc.xml


          <ejb-relation>
          <ejb-relation-name>business-clients</ejb-relation-name>

          <foreign-key-mapping/>

          <ejb-relationship-role>
          <ejb-relationship-role-name>business-has-clients</ejb-relationship-role-name>
          <fk-constraint>true</fk-constraint>
          <key-fields/>
          </ejb-relationship-role>
          <ejb-relationship-role>
          <ejb-relationship-role-name>client-of-business</ejb-relationship-role-name>
          <key-fields>
          <key-field>
          <field-name>id</field-name>
          <column-name>business_id</column-name>
          </key-field>
          </key-fields>
          </ejb-relationship-role>
          </ejb-relation>


          Here "business_id" is the dummy field CMP will use to maintain the link for you and "id" is the primary key field in the "to" end of the relation link ( ie business ejb / table ).

          What this error says ( from my experience ) is that there exists a relation defined in ejb-jar.xml between two entities that you have not told cmp how the mapping should work by specifying fields in at least one end of the relationship mapping in the jbosscmp-jdbc.xml file.

          As way of some usefull background info have a read of

          http://otn.oracle.com/tech/java/oc4j/doc_library/902/ejb/ormap.htm

          Oracle's tutorial implies some containers can do this automatically but experience to date implies that JBoss needs a cmp entry.

          Hope this helps.

          • 2. Re: As always, relationships problems
            Harald Newbie

            Thank you for your post, I have followed up on your tips and it did clarify a couple of issues, put I still have problems.
            The most frustrating one is that Jboss seems to ignore my jbosscmp-jdbc.xml files, even when I remove them I get the same result:
            WARN [org.jboss.system.ServiceController] Problem starting service jboss.j2ee:jndiName=local/MyContactEJB,service=EJBorg.jboss.deployment.DeploymentException: Atleast one role of a foreign-key mapped relationship must have key fields: ejb-relation-name=MyAdd.

            There is a couple of things that are not clear in my post, the first one is that the CMR filed “address” is an object.
            Second I have a hireky of JARs. One “main” JAR and several “help” JARs (same structure as in the Petshop)

            My though is that I should have one jbosscmp-jdbc.xml per JAR (if it includes relationships in that JAR as defined in the ejb-jar.xml), in other words no relationships in the main JAR's bosscmp-jdbc.xml.
            But I have a couple of questions since I suspect that one of my problems may be that my jbosscmp-jdbc.xml are faulty:
            1) Do I need to include datasource in all jbosscmp-jdbc.xml, even if I have not defined any SQL on “help” JAR level?
            2) And must <enterprise-beans> be repeated in the bosscmp-jdbc.xmlfile?

            One more thing, is it really worth the effort to have CMR fields instead of handling the relations in the database tables directly and map everything as CMPs, the only problem I can see with that is that I have to pre-define the tables in the DB?

            • 3. Re: As always, relationships problems
              Harald Newbie

              Thank you for your post, I have followed up on your tips and it did clarify a couple of issues, put I still have problems.
              The most frustrating one is that Jboss seems to ignore my jbosscmp-jdbc.xml files, even when I remove them I get the same result:
              WARN [org.jboss.system.ServiceController] Problem starting service jboss.j2ee:jndiName=local/MyContactEJB,service=EJBorg.jboss.deployment.DeploymentException: Atleast one role of a foreign-key mapped relationship must have key fields: ejb-relation-name=MyAdd.
              here is a couple of things that are not clear in my post, the first one is that the CMR filed “address” is an object.
              Second I have a hireky of JARs. One “main” JAR and several “help” JARs (same structure as in the Petshop)

              My though is that I should have one jbosscmp-jdbc.xml per JAR (if it includes relationships in that JAR as defined in the ejb-jar.xml), in other words no relationships in the main JAR's bosscmp-jdbc.xml.
              But I have a couple of questions since I suspect that one of my problems may be that my jbosscmp-jdbc.xml are faulty:
              1) Do I need to include datasource in all jbosscmp-jdbc.xml, even if I have not defined any SQL on “help” JAR level?
              2) And must <enterprise-beans> be repeated in the bosscmp-jdbc.xmlfile?
              One more thing, is it really worth the effort to have CMR fields instead of handling the relations in the database tables directly and map everything as CMPs, the only problem I can see with that is that I have to pre-define the tables in the DB?

              • 4. Re: As always, relationships problems
                Harald Newbie

                Thank you for your post, I have followed up on your tips and it did clarify a couple of issues, put I still have problems.
                The most frustrating one is that Jboss seems to ignore my jbosscmp-jdbc.xml files, even when I remove them I get the same result:
                WARN [org.jboss.system.ServiceController] Problem starting service jboss.j2ee:jndiName=local/MyContactEJB,service=EJBorg.jboss.deployment.DeploymentException: Atleast one role of a foreign-key mapped relationship must have key fields: ejb-relation-name=MyAdd.
                here is a couple of things that are not clear in my post, the first one is that the CMR filed “address” is an object.
                Second I have a hireky of JARs. One “main” JAR and several “help” JARs (same structure as in the Petshop)

                My though is that I should have one jbosscmp-jdbc.xml per JAR (if it includes relationships in that JAR as defined in the ejb-jar.xml), in other words no relationships in the main JAR's bosscmp-jdbc.xml.
                But I have a couple of questions since I suspect that one of my problems may be that my jbosscmp-jdbc.xml are faulty:
                1) Do I need to include datasource in all jbosscmp-jdbc.xml, even if I have not defined any SQL on “help” JAR level?
                2) And must <enterprise-beans> be repeated in the bosscmp-jdbc.xmlfile?
                One more thing, is it really worth the effort to have CMR fields instead of handling the relations in the database tables directly and map everything as CMPs, the only problem I can see with that is that I have to pre-define the tables in the DB?

                • 5. Re: As always, relationships problems
                  Harald Newbie

                  Thank you for your post, I have followed up on your tips and it did clarify a couple of issues, put I still have problems.
                  The most frustrating one is that Jboss seems to ignore my jbosscmp-jdbc.xml files, even when I remove them I get the same result:
                  WARN [org.jboss.system.ServiceController] Problem starting service jboss.j2ee:jndiName=local/MyContactEJB,service=EJBorg.jboss.deployment.DeploymentException: Atleast one role of a foreign-key mapped relationship must have key fields: ejb-relation-name=MyAdd.

                  There is a couple of things that are not clear in my post, the first one is that the CMR filed “address” is an object.
                  Second I have a hireky of JARs. One “main” JAR and several “help” JARs (same structure as in the Petshop)

                  My though is that I should have one jbosscmp-jdbc.xml per JAR (if it includes relationships in that JAR as defined in the ejb-jar.xml), in other words no relationships in the main JAR's bosscmp-jdbc.xml.
                  But I have a couple of questions since I suspect that one of my problems may be that my jbosscmp-jdbc.xml are faulty:
                  1) Do I need to include datasource in all jbosscmp-jdbc.xml, even if I have not defined any SQL on “help” JAR level?
                  2) And must <enterprise-beans> be repeated in the bosscmp-jdbc.xmlfile?

                  One more thing, is it really worth the effort to have CMR fields instead of handling the relations in the database tables directly and map everything as CMPs, the only problem I can see with that is that I have to pre-define the tables in the DB?

                  • 6. Re: As always, relationships problems
                    Harald Newbie

                    Thank you for your post, I have followed up on your tips and it did clarify a couple of issues, put I still have problems.
                    The most frustrating one is that Jboss seems to ignore my jbosscmp-jdbc.xml files, even when I remove them I get the same result:
                    WARN [org.jboss.system.ServiceController] Problem starting service jboss.j2ee:jndiName=local/MyContactEJB,service=EJBorg.jboss.deployment.DeploymentException: Atleast one role of a foreign-key mapped relationship must have key fields: ejb-relation-name=MyAdd.

                    There is a couple of things that are not clear in my post, the first one is that the CMR filed “address” is an object.
                    Second I have a hireky of JARs. One “main” JAR and several “help” JARs (same structure as in the Petshop)

                    My though is that I should have one jbosscmp-jdbc.xml per JAR (if it includes relationships in that JAR as defined in the ejb-jar.xml), in other words no relationships in the main JAR's bosscmp-jdbc.xml.

                    But I have a couple of questions since I suspect that one of my problems may be that my jbosscmp-jdbc.xml are faulty:
                    1) Do I need to include datasource in all jbosscmp-jdbc.xml, even if I have not defined any SQL on “help” JAR level?
                    2) And must <enterprise-beans> be repeated in the bosscmp-jdbc.xmlfile?
                    3) How do I include JARs in the jbosscmp-jdbc.xml?

                    One more thing, is it really worth the effort to have CMR fields instead of handling the relations in the database tables directly and map everything as CMPs, the only problem I can see with that is that I have to pre-define the tables in the DB and add a few more tables to handle the relations?

                    • 7. Re: As always, relationships problems
                      Harald Newbie

                      Ups....Hm is there a buglist for the forum????

                      • 8. Re: As always, relationships problems
                        Tim Newbie

                        a) Regarding your questions about structure of jbosscmp-jdbc.xml. These questions can be answered by
                        reviewing the file jboss-3.0.4_tomcat-4.1.12/docs/dtd/jbosscmp-jdbc_3_0.dtd.

                        The DTD file definitions of what is mandatory and what isn't.

                        There are many references to DTD sytax available on the web.

                        b) "How do I included jars in the jbosscmp-jdbc.xml" -- Huh?

                        c) "Is it really worth the effort to have CMR fields?"

                        Well this depends on your point of view. I say yes.
                        The EJB is an abstraction of your data / business logic.
                        The fact that the data is persisted into a relational db
                        is somewhat consequential. I prefer to code to an object model not a relational model.
                        Keep in mind the following:
                        1) The ejb-jar.xml defines the object level stuff.
                        2) the jbosscmp-jdbc.xml provides "extra" info to the
                        container to say how things should be mapped into a database. Somewhat like a "tuning" file. The problem here is that we have what is referred to as "abstraction leakage". In other words we have this nifty way of defining and building objects, but have to get our hands a tad dirty by having to be aware of the fact that ejb's are mapped to an RDB. My preference would be to scrap RDB as a persistence engine and use an object db ( Cache, Ozone etc). This would then do away with the messy jdbc mapping file.

                        Creating fk fields in your ejb would not be good from a design / maintenance perspective. As you mentioned the "Address" field is an object. Placing key fields etc in the EJB that references address will cause more grief that its worth.

                        Lets suppose you have EJB Person and EJB Address.
                        The Address will be stored in an address table and abstracted by the address EJB. The Person EJB would have a CMR field called "address" with "Address getAddress()" and "setAddress(Address address)". There would be no "address_fk_id" or such like in the Person EJB. The address_fk_id field is defined only in the the jbosscmp-jdbc.xml file. The definition appears only so that the container knows when you call getAddress() how to return an address object. It knows how to do this at the RDB level by doing a join. There should be no need to add / predefine more tables to your model to accomodate this.

                        I would very much recommend you lay your hands on a copy of "Profession EJB" it's a Wrox publication ISBN - 1-861005-08-3. This book helped me to keep my sanity.
                        This book has an excellent section on CMR and oodles of other usefull info. Not cheep but good value.

                        • 9. Re: As always, relationships problems
                          Tim Newbie

                          You might like to look at the following:

                          http://www.software-friends.com/know_how/content02.htm

                          • 10. Re: As always, relationships problems
                            charlie charlie Newbie

                            1) the relationship between the two bean is one-one, so in
                            jbosscmp-jdbc.xml file , you should declare the
                            <key-fields>
                            <key-field>address</key-field>
                            </key-fields>

                            to the <ejb-relationship-role-name>AddressEJB</ejb- relationship-role-name>
                            this is you error show .


                            • 11. Re: As always, relationships problems
                              charlie charlie Newbie

                              1) the relationship between the two beans is one-one, so in jbosscmp-jdbc.xml file , you should declare the
                              <key-fields>
                              <key-field>primary-key</key-field>
                              </key-fields>

                              to the <ejb-relationship-role-name>AddressEJB</ejb- relationship-role-name>

                              this is your error show .

                              • 12. Re: As always, relationships problems
                                Harald Newbie

                                All...a BIG than you. Now I know how to move forward with the config.
                                I am moving out of the dark realm of ignorance

                                • 13. Re: As always, relationships problems
                                  Henrik Andersson Newbie

                                  Hi,

                                  I'm experiencing a similar problem I think:

                                  "Atleast one role of a foreign-key mapped relationship must have key fields:"

                                  My question is: How do I make XDoclet to handle this relation?

                                  It's a one-to-one relation between Person and ContactInfo.

                                  /**
                                  * @return contactinfo of this person
                                  *
                                  * @ejb:interface-method view-type="local"
                                  * @ejb:relation
                                  * name="person-contactinfo"
                                  * role-name="one-person-has-one-contactinfo"
                                  * target-role-name="one-contactinfo-belongs-to-one-person"
                                  * target-ejb="ContactInfo"
                                  * target-multiple="no"
                                  * @jboss:target-relation related-pk field="contactinfo_id"
                                  * fk-column="contactinfo_id_fk"
                                  */

                                  I'm running JBoss 3.0.4 and XDoclet 1.1.2

                                  Thank you.