1 Reply Latest reply on Dec 24, 2008 6:06 AM by Nicole Schweighardt

    How to delete relations in entities, id´s are not the

    Nicole Schweighardt Novice

      Hi,

      I have a webapplication with jsf and ejb.If I try to delete relations between entities I always run into the same problem. The id of the objects are not the same so I can delete them from the list.

      For example:
      Relation Course/CmtUser

      Course.java

      @ManyToMany(fetch=FetchType.EAGER)
      @IndexColumn(name="INDEX_COL")
      private List<CmtUser> participants = new ArrayList<CmtUser>();


      CmtUser.java

      @ManyToMany(mappedBy="members", fetch=FetchType.EAGER)
      @IndexColumn(name="INDEX_COL")
      private List <CmtGroup> groups = new ArrayList<CmtGroup>();


      Joining a course is not the problem:

      JSF-managedbean CourseBean:

      public void joinCourse(){
       currentUser = userHandler.getCmtUserByUserName(getRemoteUserName());
      courseHandler.joinCourse(getCurrentCourseId(),currentUser);
      }


      But if I try to "leave a course" (delete user from course-participant-list) it does not work.
      I tried it in several ways:
      1. Way
      The managedbean CourseBean invokes the handler and gives the objects to it:
      public void leaveCourse(){
      courseHandler.leaveCourse(currentCourse, currentUser);
      }

      Then in the handler I tried to remove the user from the course and vice versa
      public void leaveCourse(Course c, CmtUser user) {
       c.getParticipants().remove(user);
       user.getCourses().remove(c);
       em.merge(c);
       em.merge(user);
       }


      Problem: Cannot remove the user from c. participants because the id from the user I not the same as the user in the list participants.

      The same problem in the 2. Way

      2. Way
      The managedbean CourseBean invokes the handler:
      public void leaveCourse(){
      courseHandler.leaveCourse(currentCourse.getId(), currentUser.getId());
      }


      CourseHandler.java

      public void leaveCourse(Long courseId, Long userId) {
       Course course = getCourse(courseId);
       CmtUser user = userHandler.getCmtUser(userId);
      
       course.getParticipants().remove(user);
       user.getCourses().remove(course);
       em.merge(user);
       em.merge(course);
      }


      If I look into course.participants the id from the user in it is another than the id from the user I got with my userHandler with userHandler.getCmtUser(userId);.

      3. Way:

      I remove the elements from the list in the jsfmanagedbean CourseBean:

      public void leaveCourse(){
       currentCourse.getParticipants().remove(currentUser);
       currentUser.getCourses().remove(currentCourse);
       courseHandler.updateCourse(currentCourse);
       userHandler.updateUser(currentUser);
      }


      The only thing I do in the ejbhandler is to update the objects:
      CourseHandler.java
      public void updateCourse(Course course) {
       em.merge(course);
      }
      

      UserHandler.java
      public void updateUser(CmtUser cmtUser) {
       em.merge(cmtUser);
      }


      But it is the same problem.

      How do I solve this? Which is the best way to implement this things; in the jsfmanagedbean or in the ejbhandlers?

      Please I need your help.

      Thanks Nicki