6 Replies Latest reply on May 31, 2004 11:23 AM by hbaxmann

    Foreign key set to NULL

    benzam

      Hello,

      I am using CMR and have encountered a case where the foreign key violation in the database is not thrown as an exception by Jboss. The details are as follows...

      I have a one to one relationship between two entities with a foreign key constraint in the database.
      On trying to delete the parent entity when the child entity(one with the foreign key) still has a valid reference to it (the FK) I was expecting an exception...that it cannot be deleted.
      However, each time I call the remove() method the parent entity (row in database) is deleted and the foreign key in the child entity(row in database) is set to NULL.

      This occurs only when I use entity beans with Jboss. The same code works as expected with other App servers.

      I am using Jboss 3.2.1 version and DB2 as the database.

      Any idea?
      Thanks.

        • 1. Re: Foreign key set to NULL
          mbarragan

          I have the same problem...

          I am running jboss-3.2.2RC3.

          any idea?????

          • 2. Re: Foreign key set to NULL
            jbm

            Please, if one of you had an answer since, could you post it ?

            Thanks

            • 3. Re: Foreign key set to NULL
              aloubyansky

              This is the intended behaviour. If a referenced entity can't be removed you have to check references yourself.

              • 4. Re: Foreign key set to NULL
                jbm

                Hi,

                Thanks for your answer.
                It's quite interresting behaviour.
                I'd like to understand a bit farther, is there any doc where this behaviour is detailed ?

                Or is there a simple and specific reason for it ?

                Thanks.

                Jbm

                • 5. Re: Foreign key set to NULL
                  aloubyansky

                  A simple reason is to be able to remove w/o exception from the db.

                  As for me, there is no strict theoretical prove which way is right here. The default behaviour should the one that is expected in the majority of cases.

                  • 6. Re: Foreign key set to NULL
                    hbaxmann

                    It is a question of cardinality of the relationship _end_. Set a NOT NULL constraint on the FK will give you the exception. The full cardinality of the relation will then be: (Entity A)1:1 <--is a--> 1:1(Entity B) which means: "one and exactly one is A one and exactly one B" in contrast to "one or no A is one or no B": 0:1<--is a-->0:1.

                    JBoss has obviously in your case the 0 as default for the lower boundary of relationship ends. But this should come from the NOT NULL attribute of DatabaseMetadata.

                    bax