4 Replies Latest reply on Jul 10, 2003 2:13 PM by studenik

    JBoss-3.2.1 CMR cascade-delete order

    studenik

      Hello,

      - I am using CMR with JBoss-3.2.1.
      - It is working fine.
      - I use cascade-delete feature.
      - It is working if there is no foreign key constraint.
      - If there is a foreign key constraint it is not woking, because the container first tries to delete the row in the parent table and then, tries to delete all related records in the child table(s). The database does not allow deleting a parent record if there is at least one related child record.

      My question is:
      How can I change this behaviour so that the child records are deleted first and then the parent ones? Otherwise, cascade-delete does not make much sense, I still have to delete all child records one by one and then delete the parent record.

      My configuration (1 Customer to many Users):

      ejb-jar.xml:
      ....
      <!-- Relationships -->

      <ejb-relation >
      <ejb-relation-name>Customer-User</ejb-relation-name>

      <ejb-relationship-role >
      <ejb-relationship-role-name>customer-has-users</ejb-relationship-role-name>
      One
      <relationship-role-source >
      <ejb-name>Customer</ejb-name>
      </relationship-role-source>
      <cmr-field >
      <cmr-field-name>users</cmr-field-name>
      <cmr-field-type>java.util.Collection</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>

      <ejb-relationship-role >
      <ejb-relationship-role-name>user-belongs-to-customer</ejb-relationship-role-name>
      Many
      <cascade-delete/>
      <relationship-role-source >
      <ejb-name>User</ejb-name>
      </relationship-role-source>
      </ejb-relationship-role>

      </ejb-relation>

      ...

      jbosscmp-jdbc.xml:
      ...

      <ejb-relation>
      <ejb-relation-name>Customer-User</ejb-relation-name>

      <foreign-key-mapping/>

      <ejb-relationship-role>
      <ejb-relationship-role-name>customer-has-users</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>customerId</field-name>
      <column-name>CustomerID</column-name>
      </key-field>
      </key-fields>

      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>user-belongs-to-customer</ejb-relationship-role-name>
      <key-fields/>

      </ejb-relationship-role>
      </ejb-relation>

      ...

      Thanks a lot!

        • 1. Re: JBoss-3.2.1 CMR cascade-delete order
          samart

          i've got the same exact issue...

          anyone know why?
          Thanks.

          • 2. Re: JBoss-3.2.1 CMR cascade-delete order
            studenik

            I am looking for any possibility to specify the order of cascade-delete. Is it possible to tell the container to delete child records first and then delete the parent target? I do not ask for the solution. I just ask is it possible or not.

            Thanks and cheers!

            • 3. Re: JBoss-3.2.1 CMR cascade-delete order
              samart

              if you turn on the CMP debug output, you can see what Jboss does.

              it first nulls all the FK's. then deletes the tables...so if they do that, then delete order doesnt matter. The problem is, as i discovered, if the child table has the FK as a PK. clearly you cant null the PK...and then the cascade delete bombs. my workaround : i'm removing the FK consraint on my pk, and adding another non pk column as the fk, so it can be nulled. messy, but it does roll back should things go awry.

              its kinda counter intuitive that they null all the child FK's, cuz thats a bunch of updates, and then deletes...when if it just deleted in the right order to being with, there would be no issue, and less db activity. i guess the question is, how can the container discern the table dependencies? maybe thats why they null the fk's, then delete in any order..maybe it cant determine the delete order..

              i agree with you, that it would be nice to specify the delete order..even nicer if it just figured it out for you.

              • 4. Re: JBoss-3.2.1 CMR cascade-delete order
                studenik

                Hi samart,

                Thank you for your comments. For now, I will be deleting children from application until a container based feature is discovered/developed. I am glad I was not alone in looking for this feature.

                Cheers!