1 Reply Latest reply on Apr 18, 2004 5:19 PM by triathlon98

    CMR parent-child : Delete followed by add fails?

    davout

      I have a problem trying to delete and then add new entities all in one call.

      The situation is something like the following...

      A 'Pupil' entity is associated with one or more 'subject' entities. I have a 'Pupil' entity bean, 'Subject' entity bean and a linking 'PupilSubjectLink' entity bean. This last bean has two fields 'PupilID' and 'SubjectID', both set as keys.

      I have a CMR one-to-many relationship set up from the 'Pupil' entity to the 'PupilSubjectLink' entity - called 'subjects'.

      When processing updates to the subject settings my code first deletes all subject links for the nominated pupil and then adds in the new subjects.

      But the process fails as the first delete operation doesn't seem to work.

      My delete code is something like the following....

      Pupil aPupil = PupilHome.findBy......
      Iterator anIterator = aPupil.getSubjects();
      while (anIterator.hasNext()) {
      PupilSubjectLink aLink = () anIterator.next();
      aLink.remove();
      }
      aPupil.getSubjects().clear(); // is this necessary?

      But this doesn't work.

      For example, if pupil 'A' starts in a situation where they are associated with subjects 'X' and 'Y', but then this is changed so that they are only associated with subject 'Y' then my app is blowing up on the final step when adding a link to subject 'Y'.

      Any ideas?

        • 1. Re: CMR parent-child : Delete followed by add fails?
          triathlon98

          Without more details (error messages), it is hard to predict the problem.

          However, it is possible that the insert is being done before the delete and that this is refused by the constraints on the DB.
          I would image this is possible when deletes are batched (there is a settig for this).

          You can always try to enable debug logging to see the SQL queries being executed (or even trace logging to see the paramaters to the SQL queries).
          The package for the CMP stuff is "org.jboss.ejb.plugins".

          There are some possible solutions. You can switch off the batching of deletes if it was switched on. Alternatively, do the deletes in a separate transaction if possible.

          Joachim