3 Replies Latest reply on Jun 30, 2002 1:59 PM by Dain Sundstrom

    setting up relationships without specifying foreign key?

    Travel Maniac Newbie

      I have a User and a Group beans. It's a N:1 relationship. So first I have a problem understanding the concept of doing this in CMP. In the ejb-jar.xml nowhere do I specify that in the user table the group_id matches with the id in the group table. And as far as I know, this info is not specified in any of the jboss xml files. So how does the system know where to insert the group_id when I try to create a User object? In the relationship section of ejb-jar.xml, I just present the relationship, declaring that cmr-field in User called group is based on the Group bean. However, I am not allowed to do User.setGroup in the ejbCreate(), nonetheless, I checked the log and it is attempting to do an insert into the user table with a column called group, which doesn't exist (I don't know what it is trying to insert into this mystery column).

      So my question is how do I specify a 1:N relationship where any of the N-objects, in this case, User, is intelligent enough to insert the id of a given Group object into its table? And also, why is it trying to insert into a column named "group" when I have no such declared cmp-field. I do have a "User.getGroup()" and "User.setGroup()", but "group" is a cmr field, not a table column.


        • 1. Re: setting up relationships without specifying foreign key?
          Dain Sundstrom Master

          I'm sorry, I can't follow your post. Can you try rewriting it? What exactally are you trying to do that doesn't work?

          • 2. Re: setting up relationships without specifying foreign key?
            Travel Maniac Newbie

            OK, let me simplify the problem.

            I have a User bean and a Group bean. For each Group bean object there are N User bean objects. In the table user, there is a group_id foreign key (a column) that links to the id column of the group table. In the User bean, I declare get/set for Group (and not GroupId). But this is not a column but a relationship. I declare this relationship in the ejb-jar.xml. In spite of this, when I try to create a User bean object, I get a sql exception. I checked the log and found out that the insert statement is attempting to insert some info into a column called "group"; I don't have a <cmp-field> called "group", so I believe that jboss is misled into believing that my get/setGroup implies existence of a group column. There is no group column (because there is no group field), there's a group_id column.

            So how do I have a User bean declare a relationship with Group class, but I don't have a "group" column in the user table; I have a group_id column that would help the User bean retrieve info about a Group.

            So it's hard for me to explain in a message that doesn't bore people to death. Is there an example somewhere for JBoss3.0 using CMP2.x indicating how to create relationships based on foreign keys? I don't want to get the docs yet until I know they are for JBoss3.0. I looked for examples on the Net but they don't meet my specific needs.


            • 3. Re: setting up relationships without specifying foreign key?
              Dain Sundstrom Master

              JBossCMP will automatically geneate a database mapping in order to store the entity in the database. If you want a specific mapping, which is what you are asking for, then you must specify it with a jbosscmp-jdbcm.xml file. The structure of this file is detailed in the for pay JBossCMP documentation and in the jbosscmp-jdbc_3_0.dtd file in the docs/dtd directory of the jboss distribution.

              If you get the JBoss test suite from cvs you can find some working files.