3 Replies Latest reply on Jun 18, 2003 10:59 AM by darranl

    cascade-delete doesn't work

    pdelgado0

      Hi,
      I'm using postgres 7.3.2 and jboss 3.2 and my problem is that the tag <cascade-delete/> in a one-to-many relationship doesn't work

      A (1) ----- (*) B

      I have my relation like this in the ejb-jar.xml

      <ejb-relation> <ejb-relation-name>A-B</ejb-relation-name>
      <ejb-relationship-role> <ejb-relationship-role-name>A-belongs-to-B</ejb-relationship-role-name>
      Many
      <cascade-delete/>
      <relationship-role-source >
      <ejb-name>A</ejb-name>
      </relationship-role-source>
      <cmr-field >
      <cmr-field-name>token</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>

      <ejb-relationship-role> <ejb-relationship-role-name>B-has-Aes</ejb-relationship-role-name>
      One
      <relationship-role-source >
      <ejb-name>B</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>availabilities</cmr-field-name>
      <cmr-field-type>java.util.Collection</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>

      </ejb-relation>

      and in the jbosscmp-jdbc.xml ...

      <ejb-relation>
      <ejb-relation-name>A-B</ejb-relation-name>
      <foreign-key-mapping/>

      <ejb-relationship-role> <ejb-relationship-role-name>A-belongs-to-A</ejb-relationship-role-name>
      <fk-constraint>true</fk-constraint>
      <key-fields/>
      </ejb-relationship-role>

      <ejb-relationship-role> <ejb-relationship-role-name>B-has-Aes</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>idtoken</field-name>
      <column-name>id_token_fk</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>

      </ejb-relation>

      and it creates the table A in my Postgres DB like this ...

      Table "public.A"
      Column | Type | Modifiers
      -------------+---------+-----------
      id_token_fk | bigint | not null
      occupation | integer | not null
      Indexes: pk_A primary key btree (id_token_fk, occupation)
      Foreign Key constraints: fk_useoccupation_token FOREIGN KEY (id_token_fk) REFERENCES B (id_token) ON UPDATE NO ACTION ON DELETE NO ACTION

      but it should have created it with this CONSTRAINT ...

      Foreign Key constraints: fk_useoccupation_token FOREIGN KEY (id_token_fk) REFERENCES B (id_token) ON UPDATE CASCADE ON DELETE NO CASCADE

      It's like the tag <cascade-delete/> didn't work.

      Please, I'd appreciate any help.

      Thanks.

      Pablo



        • 1. Re: cascade-delete doesn't work
          pdelgado0

          Upss! I've just noticed that the example I posted

          A (1) --- (*) B

          is wrong, the correct one is ...

          B (1) ---- (*) A

          • 2. Re: cascade-delete doesn't work
            pdelgado0

            What I did to solve this problem probably is not the optimal solution but at least it words.
            I created a little shell script that makes an ALTER TABLE directly on the DATABASE and change the CONSTRAINT from

            Foreign Key constraints: fk_useoccupation_token FOREIGN KEY (id_token_fk) REFERENCES B (id_token) ON UPDATE NO ACTION ON DELETE NO ACTION

            to ...

            Foreign Key constraints: fk_useoccupation_token FOREIGN KEY (id_token_fk) REFERENCES B (id_token) ON UPDATE CASCADE ON DELETE CASCADE

            so when I delete a row in the 1-side relationship, it automatically deletes the rows on the *-side relationship with no problem and in the same transaction.

            As I said above, it's not optimal but works.

            • 3. Re: cascade-delete doesn't work
              darranl

              Have you tried deleting any of the entities on the one side of your relationship?

              I think that JBoss manages the cascading delete itself instead of setting during table creation.