0 Replies Latest reply on May 10, 2006 7:00 AM by Jason Cunningham

    Why does Remove causes all CMR entities to be loaded

    Jason Cunningham Newbie

      Hi all,

      I've come across some interesting CMP/CMR behaviour in JBoss 3.2.6.

      To explain, let me define a scenario. Suppose we have two entities, 'Person' and 'PhoneNumber'. We define a CMR relationship between them such that a 'Person' can have relationships with multiple 'PhoneNumber' entities and each 'PhoneNumber' can be associated with multiple 'Person' entities.

      PhoneNumber
      ID Number
      1 07713
      2 87383
      
      Person
      Name FullName
      Bob Robert
      Mary Mary
      
      Person_PhoneNumber
      PhoneNumber Person
      1 Bob
      1 Mary
      2 Bob
      

      If we now want to delete a particular PhoneNumber entity (eg. 1), we would locate it (using findByPrimaryKey or something) and then call it's 'remove' method. What I'm finding is that the SQL that is generated is:

      select ID, number from PhoneNumber where ID = 1; ##Find by PK
      
      ## Generated as a result of the 'remove'
      select Person from Person_PhoneNumber where PhoneNumber = 1;
      
      select Name, FullName from Person where (Name=Bob) OR (Name=Mary);
      
      delete from Person_PhoneNumber where (PhoneNumber=1) AND (Name=Bob OR Name=Mary);
      
      delete from PhoneNumber where ID = 1;
      
      


      Why are the related entities loaded (ie. why is anything from the 'Person' table loaded)? Is there any way to stop this and hence speed up the process by avoiding the unnessary load?

      What I would expect is SQL along the lines of:

      select ID, number from PhoneNumber where ID = 1; ## Find by PK
      
      ## Generated as a result of the 'remove'
      delete from Person_PhoneNumber where PhoneNumber = 1;
      
      delete from PhoneNumber where ID = 1;
      
      


      Any ideas or comments would be greatly appreciated :O)

      Thanks in advance,

      Jason