4 Replies Latest reply on Oct 25, 2002 6:06 PM by alexis

    Get foreign key value

    alexis

      Hi all,

      I have an One to Many relationship:

      ejb-jar.xml:
      <ejb-relation>
      <ejb-relation-name>User-GalleryImage</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>
      User-has-many-GalleryImages
      </ejb-relationship-role-name>
      One
      <relationship-role-source>
      <ejb-name>User</ejb-name>
      </relationship-role-source>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>
      GalleryImage-has-a-User
      </ejb-relationship-role-name>
      Many
      <relationship-role-source>
      <ejb-name>GalleryImage</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>user</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      </ejb-relation>

      jbosscmp-jdbc.xml:
      <ejb-relation>
      <ejb-relation-name>User-GalleryImage</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>
      User-has-many-GalleryImages
      </ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>userID</field-name>
      <column-name>userID</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>
      GalleryImage-has-a-User
      </ejb-relationship-role-name>
      <key-fields/>
      </ejb-relationship-role>
      </ejb-relation>


      My tables look like:

      CREATE TABLE user (
      userID int(11) NOT NULL default '0',
      email char(64) NOT NULL default '',
      password char(64) NOT NULL default '',
      PRIMARY KEY (userID),
      UNIQUE KEY email_idx (email)
      ) TYPE=InnoDB;

      CREATE TABLE galleryimage (
      imageID int(11) NOT NULL default '0',
      file char(64) NOT NULL default '',
      userID int(11) default NULL,
      PRIMARY KEY (imageID),
      KEY userID_idx (userID)
      ) TYPE=InnoDB;


      When I run query using the foreign key like:
      SELECT OBJECT(gi) FROM galleryimage gi WHERE gi.user.file=?1
      My relationship is working fine.

      But when I try to insert a new row into galleryimage via GalleryImage Bean, JBoss executes the following query to do the job:
      INSERT INTO galleryimage (imageID, file, userID) VALUES (?, ?, ?)

      But JBoss always put NULL into userID column. How can JBoss put the correct foreign key value?

      Thanks,
      Alexis

        • 1. Re: Get foreign key value
          alexis

          Can anybody help me?

          This issue freezes my project.

          Thanks in advance,
          Alexis

          • 2. Re: Get foreign key value
            alexis

            Hi all,

            I have fixed this issue. I can set userID value via the foreign key into ejbPostCreate:

            public void ejbPostCreate(GalleryImageDTO gidto)
            throws CreateException
            {

            try
            {
            InitialContext ctx = new InitialContext();

            // Get user bean
            UserLocalHome uhome = (UserLocalHome)ctx.lookup("User");
            UserLocal user =
            uhome.findByPrimaryKey(new Integer(gidto.getUserID()));

            // Set foreign key value
            setUser(user);
            }
            catch(Throwable e)
            {
            throw new EJBException(e.getMessage());
            }

            }


            Alexis

            • 3. Re: Get foreign key value
              nitin888

              Hi,

              I'm sorry but i'm not being much help. Actually even i'm facing the same problem. Could anyone please reply back.

              regards,
              Nitin.

              • 4. Re: Get foreign key value
                alexis

                Hi Nitin,

                Could you provide your deployment descriptors and Bean files. Perhaps I can help you.

                Cheers,
                Alexis