3 Replies Latest reply on Sep 30, 2005 2:17 AM by justinb

    Persistence: adding to a Collection

    justinb Newbie

      Hi

      I have an Entity called "Course" (as in a training course), it looks basically like so:

      @Entity
      @Table(name = "training_course")
      public class Course implements Serializable {
      ...
       private Collection<Course> prerequisites;
      ...
       @ManyToMany(fetch = LAZY)
       @JoinTable(
       table = @Table(name = "training_course_req"),
       joinColumns = { @JoinColumn(name = "course") },
       inverseJoinColumns = { @JoinColumn(name = "prerequisite") })
       public Collection<Course> getPrerequisites() {
       return prerequisites;
       }
      
       public void setPrerequisites(Collection<Course> prerequisites) {
       this.prerequisites = prerequisites;
       }
      ...
      }


      Now I want to have a Stateless that adds and removes from this Collection
      like so...

      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      @RolesAllowed( { BasicRoles.SUPERUSER, Roles.TRAINING_MANAGER })
      public boolean addPrerequisite(String courseCode, String preCode) {
       log.debug("Adding Prerequisite " + preCode + " to " + courseCode);
       boolean success = false;
       Course course = entities.find(Course.class, courseCode);
       if (course != null) {
       log.debug("The course is " + course.getName());
       Course prerequisite = entities.find(Course.class, preCode);
       if (prerequisite != null) {
       log.debug("The prerequisite is " + prerequisite.getName());
       success = course.getPrerequisites().add(prerequisite);
       entities.merge(course);
       log.debug("Add was " + (success ? "successful" : " unsuccessful"));
       } else
       log.debug("Prerequisite is null");
       } else
       log.debug("Course is null!");
       return success;
      }


      According to the logs, all the code executes correctly, but when I look at the table, nothing has happened!

      What am I doing wrong???

      Thanks,
      Justin