6 Replies Latest reply on Jan 28, 2003 10:41 AM by gianluca

    Foreign key fields mapped to primary key fields in JBoss-3.2

    aloubyansky

      It's possible to map foreign key fields in one-to-many and one-to-one
      relationships to the primary key fields. All is needed is to assign the same
      names to the corresponding foreign and primary key fields.

      Relationships are assigned between ejbCreate and ejbPostCreate. This
      means that relationships are accessible in ejbPostCreate and not in ejbCreate.
      Relationships are established and removed only with creation and
      removal of entities. Modifications with abstract CMR accessors are not
      allowed as they change primary key values.

      If ONE side doesn't exist CMR on the MANY side will return null value.
      If MANY side doesn't exist CMR on the ONE side will return empty collection.

      If cascade-delete is specified then removal of the ONE side will
      remove the related MANY side. In this case, ONE side is removed first
      breaking the relationship, i.e. CMR field on the MANY side in ejbRemove will return null.

        • 1. Re: Foreign key fields mapped to primary key fields in JBoss
          tolborg

          I would like an answer to this question too.

          • 2. Re: Foreign key fields mapped to primary key fields in JBoss
            tolborg

            WebSphere Appserver 5 and WSAD5 does it this way.

            I have enclosed a zip file with two EJBS - AlphaBean and BetaBean.
            Please consider if this is the way JBoss is going to implement foreign keys mapped to primary keys.

            If you look carefully you'll notice that WebSphere uses the actual NAMES of the foreign keys and implements them as cmp fields.

            The database tables used in this example are like this:

            Tables:

            ALPHA
            aid (pk)
            name

            BETA
            aid (pk and foreign key to Alpha.aid)
            bia (pk)
            name
            The foreign key is named (f_alpha1)

            • 3. Re: Foreign key fields mapped to primary key fields in JBoss
              tolborg

              Hmm... where did that zip file go. I'll try again

              • 4. Re: Foreign key fields mapped to primary key fields in JBoss
                tolborg

                When I wrote that "I would like and answer to this question too" I has misunderstood your post. Its not a question - its a pointer on how to "do it".

                What I don't understand is that this seems to mean that different containers does this in different ways which breaks portability.
                Could you please give me a small example on how to do this. Especially the "Relationships are assigned between ejbCreate and ejbPostCreate" I don't understand.

                • 5. Re: Foreign key fields mapped to primary key fields in JBoss

                  > Especially the "Relationships are assigned
                  > between ejbCreate and ejbPostCreate" I don't
                  > understand.

                  "This means that relationships are accessible in ejbPostCreate and not in ejbCreate."

                  It is work performed by the container in between the two methods to establish the relationship.

                  • 6. Re: Foreign key fields mapped to primary key fields in JBoss
                    gianluca

                    I'm specially interested on how to do this :

                    > It's possible to map foreign key fields in
                    > one-to-many and one-to-one
                    > relationships to the primary key fields. All is
                    > needed is to assign the same
                    > names to the corresponding foreign and primary key
                    > fields.

                    What primary key field name and primary key class do you use if you implement a relation like this ?
                    The entity primary key should be made of its primary key field (if any) and one or more mapped foreign keys fields. How do you represent this in the ejb-jar.xml ?
                    How will the findByPrimaryKey() work ?
                    How to you define this kind of relationship in the jbosscmp-jdbc.xml deployment descriptor ?
                    Do you have a working sample of all this ?

                    Thanks in advance.