9 Replies Latest reply on Jan 20, 2003 9:21 PM by Ignacio de Cordoba

    Is it impossible to have a CMR field be part of the primary

    Ignacio de Cordoba Novice

      Hi there,
      in my data base structure, I have many tables in which their primary key has foreign references to other tables.
      As I have figured out, JBoss cannot handle this, basically because CMRs must be nullable.
      If I include the column both as CMP and CMR, I get a "column included twice" Postgres JDBC Exception. If I just include the column as CMR, it cannot be part of the Primary Key (the PK class for the Entity EJB) and so, I cannot remove objects (must give a PK object in the remove method in the HOME interface).

      Is there any workarround for this? Must I redesign all my structure so that data base tables have no primary keys column being part of a foreign key?

      Picture this...
      I have a newsletter EJB and a Newsletter subscriber EJB.
      My system can handle many newsletters.
      In the data base, the newsletter subscribers primary key is (email,newsletterid), but newsletterid is also foreign key to the newsletters.id table column, so must be a CMR field.
      How can I solve this?

      thanks for any ideas!!!


        • 1. Re: Is it impossible to have a CMR field be part of the prim
          t g Newbie

          I ran into the same problem. The workaround I am using is I use CMP but do not use CMR. In a session bean I manage the relationships. Not ideal, but works.

          • 2. Re: Is it impossible to have a CMR field be part of the prim
            Ignacio de Cordoba Novice

            Thanks for your reply Tommy,
            I guess I'll have to wait till JBoss handles this properly before using it in production environment. I've seen a patch to handle nullable CMRs and hope JBoss team introduces it soon in a packaged version.

            After my several months experience with JBoss, this is the only serious lack that prevents me from using it.

            • 3. Re: Is it impossible to have a CMR field be part of the prim
              Alexey Loubyansky Master

              JBoss-3.2 supports foreign key fields mapped to primary key fields. I am not sure it's currently in binary downloads on the sourceforge but upcoming release is scheduled for the 12th of January.

              • 4. Re: Is it impossible to have a CMR field be part of the prim
                Ignacio de Cordoba Novice

                Hi again and thanks for your response.
                I've downloaded JBoss 3.2beta3 and even though it seems to have solved another problem I had with transactions when using Entity EJBs directly from JSP pages, I still get the

                java.sql.SQLException: ERROR: Attribute 'newsletterid' specified more than once

                I don't know if I am doing something wrong.

                In my newsletter/newslettersubscribers simple example, I've set 2 CMP fields, email and newsletterid, and a CMR field, newsletter (which is the CMR equivalent to the newsletterid CMP). It is a not null field referencing the id field in the newsletter entity EJB.

                Do I have to do something special in the newslettersubscriber ejbCreate and ejbPostcreate methods?

                I've found a patch that don't know if is included in 3.2b3:


                support for cmr+cmp not null fields
                This patch addresses issues with fields that exist as
                both CMP fields and as part of a CMR field,
                particularly when the field is not null.

                There are two different issues addressed by these
                changes. First, there are duplicate column names in
                the insert and create table sql commands that jboss
                generates. Second, jboss attempts to insert NULL values
                for these fields, even when they are part of the
                primary key.

                The first issue is dealt with by building a list of
                unique fields to use for the create table and insert
                statements. This list of fields contains all the CMP
                fields followed by the CMR fields that are not in the
                CMP field list.

                The second issue is taken care of by the insertion of
                code to populate the bean instance values for the
                duplicated CMR fields before the sql insert parameters
                are bound. This way, when the parameter binding code
                executes, the correct values are pulled from the bean
                instance as if they had been set in ejbCreate. This is
                accomplished by invoking the "get" method on the local
                interface argument passed into ejbCreate. The way it
                works is by querying the duplicated CMR field's related
                field and extracting it's field name and it's local
                interface name. Then, the arguments to ejbCreate are
                inspected for a matching local interface name and a
                matching get method for the field name. When it finds
                a match, the get method is invoked and the return value
                is assigned to the bean instance. For this change to
                work properly, the local interfaces for the related
                fields of the duplicated CMR fields must be passed in
                as arguments to ejbCreate. Also, since the get method
                is invoked and the bean instance value is set after the
                ejbCreate call, any code in ejbCreate that sets the
                field's bean instance value will be overridden.

                This is the sequence of events.

                1) ejbCreate is called normally and returns.
                2) the patch code is called to populate duplicated CMR
                field instance values
                3) the primary key is extracted from the bean and
                stored in the container context
                4) the sql insert statement is prepared and executed.
                5) ejbPostCreate is called.

                Thanks for any help... I am getting crazy with this and need to solve it to really put JBoss in production.

                • 5. Re: Is it impossible to have a CMR field be part of the prim
                  Alexey Loubyansky Master

                  Check "[ 658296 ] FK fields mapped to PK fields support" that explains how it works.
                  If it doesn't work with the current binary download on sf.net then wait for the 12th of Jan when new release will be available.
                  Mapping a CMR field with a foreign key to a CMP field that is not a primary key is not currently supported.


                  • 6. Re: Is it impossible to have a CMR field be part of the prim
                    Ignacio de Cordoba Novice

                    Hi there,
                    I have carefully read that doc again (I had already).

                    I have removed the newsletterid CMP field from my newslettersubscriber bean and set newsletter in ejb-jar as both CMP field and CMR field.
                    My problem is in the Compound primary key class. It used to have two fields: email and newsletterid, but now newsletterid no longer exists, as the newsletter bean only has a real CMP field, email, and a CMR: newsletter.
                    I've tried to make newsletter part of the compount PK class, but it doesn't work (JBoss tries to deserialize it from the JDBC stream ;-) )

                    Does this mean that JBoss support for CMR fields in primary key is only for simple primary keys and not compount primary keys?

                    thanks a lot for your help and greetings from Madrid,

                    • 7. Re: Is it impossible to have a CMR field be part of the prim
                      Thomas Tolborg Newbie

                      loubyansky: Where can I find this "[ 658296 ] FK fields mapped to PK fields support"?

                      • 8. Re: Is it impossible to have a CMR field be part of the prim
                        t g Newbie

                        This is a question I have as well. One solution for the would be to engineer the JBoss persistence system to give the following option:

                        When you create a 'many' object, do not insert into the table until it has been added to the 'one' object. And when you insert it, include the value for the foreign key. So the result is one insert that includes the foreign key value.