3 Replies Latest reply on Apr 16, 2007 4:36 AM by fhh

    Many-to-Many and cascade pronlem ?

    Eric Chow Apprentice

      Hello,


      In the following code, User and Role. When I tried to remove User, it will remove all the roles in the ROLE that the user has and also all the related records in the USER_ROLE_LINK.

      For example,


      User u = new User();
      Role a = new Role("A");
      Role b = new Role("B");


      // persist a and b

      ArrayList roles = new ArrayList();
      roles.add(a);
      roles.add(b);

      u.setRoles(roles);

      //persist u


      .....


      em.remove(u);




      For the above codes, it wiil cause the problem that I described above.
      How can I only remove the User and the related records in the USER_ROLE_LINK when tried to remove a single user?

      And how can I remove all the users that a role have when remove a ROLE ?



      public class User {
      
      ...
      @ManyToMany (cascade=CascadeType.ALL,
       mappedBy = "users", targetEntity = Role.class)
      public Collection<Role> getRoles() {
       return roles;
      }
      
      ...
      
      }
      
      



      public class Role {
      ...
      @ManyToMany (fetch = FetchType.EAGER, targetEntity = User.class, cascade=CascadeType.REMOVE)
      @JoinTable (name = "USER_ROLE_LINK", joinColumns = {
       @JoinColumn (name = "ROLENAME")
      }, inverseJoinColumns = {@JoinColumn (name = "USERNAME")
      })
      public Collection<User> getUsers() {
       return users;
      }
      
      ...
      }
      
      





        • 1. Re: Many-to-Many and cascade pronlem ?
          fhh Expert

           

          When I tried to remove User, it will remove all the roles in the ROLE that the user has and also all the related records in the USER_ROLE_LINK.


          I would say this is the expected behavoiour. You asked to remove the user and all of his roles.


          And how can I remove all the users that a role have when remove a ROLE ?

          Have you tried removing the CasadeType.ALL and replace it with the cascade types you actually want?

          Regards

          Felix

          • 2. Re: Many-to-Many and cascade pronlem ?
            Eric Chow Apprentice

            Hi,

            I tried to use CascadeType.REMOVE, but it got the same result.

            I just want to remove the USER and its relationship when removing a user.

            Please help, I tried many ways but also failed.


            Best regards,
            Eric

            • 3. Re: Many-to-Many and cascade pronlem ?
              fhh Expert

              If you do NOT want to cascade on remove then don't set it. The cascade options refer to the Java objects NOT the representation in the database.

              The rows from the ManyToMay JoinTable will get removed WITHOUT any cascading options set because removing the User and not the rows in the join table would violate a fk constraint.

              Regards

              Felix