7 Replies Latest reply on Mar 6, 2002 3:03 AM by ppetit

    Two relationships among three entity beans

    brian Newbie

      There are three local entity beans. Two one-to-many relationships exist. That is, one local bean (C) has two foreign keys representing the other two beans (A and B).

      JBoss 3.0 alpha cannot automatically create the table (C) with two foreign keys (A' and B's IDs). However, I manually created the table C and the relationships worked fine. I have all abstract methods in the two local beans (A & B) to automatically insert foeign key ids in the table C.

      I defined relationships and set foreign keys in entity C in the files ejb-jar.xml and jbosscmp-jdbc.xml. (In jbosscmp-jdbc.xml file, I set <foreign-key-mapping>.) In addition, I set create-table as true.

      Since the relationships work fine, I believe the xml descriptors are correct except the table creation.

      Can anyone tell me if it is possible to let jboss create the table C automatically? How to do? Thanks.

      Brian

        • 1. Re: Two relationships among three entity beans
          Dain Sundstrom Master

          You lost me. Can you me more specific? Are you getting an exception? Use some better names then A, B and C.

          • 2. Re: Two relationships among three entity beans
            brian Newbie

            Thanks. Sorry for the unclear descriptions of my questions.

            Here is an excerpt from my ejb-jar.xml:

            <enterprise-beans>

            <ejb-name>company</ejb-name>
            ...
            <cmp-field>companyID</cmp-field>
            ...
            <primkey-field>companyID</primkey-field>
            ...



            <ejb-name>individual</ejb-name>
            ...
            <cmp-field>individualID</cmp-field>
            ...
            <primkey-field>individualID</primkey-field>
            ...



            <ejb-name>contact</ejb-name>
            ...
            <cmp-field>contactID</cmp-field>
            ...
            <cmp-field>companyID</cmp-field>
            <cmp-field>individualID</cmp-field>
            ...
            <primkey-field>contactID</primkey-field>

            ...
            </enterprise-beans>


            <ejb-relation>
            <ejb-relation-name>company-contact</ejb-relation-name> <ejb-relationship-role>
            <ejb-relationship-role-name>company-has-contacts</ejb-relatonship-role-name>>
            One
            <relationship-role-source><ejb-name>company</ejb-name></relationship-role-source>
            <cmr-field>
            <cmr-field-name>contactIDs</cmr-field-name>
            <cmr-field-type>java.util.Collection<cmr-field-type>
            </cmr-field>
            </ejb-relationship-role>
            <ejb-relationship-role>
            <ejb-relationship-role-name>contact-belongs-to-company</ejb-relatonship-role-name>>
            Many
            <relationship-role-source><ejb-name>contact</ejb-name></relationship-role-source>
            </ejb-relationship-role>
            </ejb-relation>


            <ejb-relation>
            <ejb-relation-name>individual-contact</ejb-relation-name> <ejb-relationship-role>
            <ejb-relationship-role-name>individual-has-contacts</ejb-relatonship-role-name>>
            One
            <relationship-role-source><ejb-name>individual</ejb-name></relationship-role-source>
            <cmr-field>
            <cmr-field-name>contactIDs</cmr-field-name>
            <cmr-field-type>java.util.Collection<cmr-field-type>
            </cmr-field>
            </ejb-relationship-role>
            <ejb-relationship-role>
            <ejb-relationship-role-name>contact-belongs-to-individual</ejb-relatonship-role-name>>
            Many
            <relationship-role-source><ejb-name>contact</ejb-name></relationship-role-source>
            </ejb-relationship-role>
            </ejb-relation>



            In my jbosscmp-jdbc.xml, I set <create-table> as true and <remove-table> as false for the contact entity. I put all entity descriptions and relationships in the jbosscmp-jdbc.xml file.

            At the beginning, I started with company and contact beans and their one-to-many relationships. Both the company and
            contact tables were created by jboss. Then, I added individual entity and the second one-to-many relationship.
            At this time, jboss did not create the contact table but remove the existing one. That is, jboss removed the contact table even I set remove-table as false. (Defualt is false as well).

            I then manually created the contact table, rum my ap. I could insert, update,and delete data in the three tables without a problem. jboss uses cmr-field abstract accessors defined in company and individual beans to automatically insert two foreing keys in the contact table.

            I know there was no exception. I can manually create the contact table. However, everytime I redeployed the ap jar. jboss removed the contact table. I have to backup the data and restore it.

            Here is my system specs for your reference:

            Mandrake Linux 8.1
            jdk1.3.1_01
            PostgreSQL 7.1.3
            jboss3.0.0alpha.

            Please help me to see if I can prevent jboss remove the table. I downloaded jboss3.0 beta and will use it. However, did anyone have a similar problem before?

            Best Regards,

            Brian






            • 3. Re: Two relationships among three entity beans
              brian Newbie



              When I redeployed my ap jar today, jboss didn't remove the contact table. It did the other day.

              However, the contact table still has to be manually created.

              Brian

              • 4. Re: Two relationships among three entity beans
                brian Newbie

                Problem Solved!

                I checked the server.log file. The problem of being unable to creating the table was because I defined the foreign key as cmp-field in the entities. Once I removed the corresponding cmp-field definitions, jboss created the table.

                • 5. Re: Two relationships among three entity beans
                  ppetit Newbie

                  Hi brian,

                  I saw you were able to make relationships working...
                  As I'm in trouble to get my own ones working.
                  Do you have some tips that could help me?

                  regards
                  Philippe

                  • 6. Re: Two relationships among three entity beans
                    brian Newbie

                    Hi,

                    The document I bought from Flashline.com under jboss category helps. You may start with a many-to-many relationship although it sounds complicated. Followings are the steps I am taking. Please someone correct me if I do something wrong.

                    1. ejb-jar.xml:
                    (1) describe your entity beans, don't define foreign keys.
                    (2) describe the relationship, specify the cmr-field matched the abstract accessors in your EJB bean.

                    2. jbosscmp-jdbc.xml:
                    (1) describe your entity beans without the foreign keys.
                    (2) describe the relationship, specify the table-key-field as the actual fields in your table to be used as foreign keys.

                    3. jboss.xml:
                    define jndi names for the beans.

                    4. your EJB object, home, and bean:
                    please match the cmr-field accessors with what you descrbe in the xml files. Please do not define the foreign key get and set methods.

                    For example, if you define customerIDs as cmr-field in your xml files, in your beans, you should define public abstract Collection getCustomerIDs(); and public abstract void setCustomerIDs(Collection customerIDs); Please note that customerIDs is not your foriegn key but a collection.
                    of your EJB objects. You may use Set instead. You should define the foreign keys in jbosscmp-jdbc.xml although jboss can automatically create for you.

                    Please consult the above mentioned documents for details. Hopefully my notes can help you avoid some mistakes I made before.

                    Best Regards,

                    Brian

                    • 7. Re: Two relationships among three entity beans
                      ppetit Newbie

                      thanks for the help

                      regards,
                      Philippe