5 Replies Latest reply on Jun 29, 2006 10:21 PM by Chris Malan

    Dissolving a many to many relationship

    Chris Malan Novice

      There is a many to many relationship between students and subjects. At the end of the year this relationship has to be dissolved so new subjects can be assigned to each student the next year.

      I thought it was going to be easy. First, get all the students in a list with a query. Then loop through this list while at the same time looping through each student

       for(Student s : students){
       s.setYear(s.getYear() + 1);
       q = manager.createQuery("select ss from Student s join s.subjects ss where s = :student");
       q.setParameter("student", s);
       List<Subject> subjects = q.getResultList();
       if(subjects != null && subjects.size() > 0){
       System.out.println("The number of subjects for student " +
       s.getNames() + " " + s.getSurname() + " is " + subjects.size());
       Iterator<Subject> iter = subjects.iterator();
       while(iter.hasNext()){
       Subject sj = iter.next(); //This is where it hits the fan
       subjects.remove(sj);
       }
       }
      


      I've also tried using a set (Set subjects = s.getSubjects()) with the same results. I also used a cast in the line with the problems.

      This is a short version of the exceptions:
      javax.ejb.EJBException: null; CausedByException is:
       null
       at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:46)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:70)
       at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
      


      There is also a
      java.util.ConcurrentModificationException
       at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
       at java.util.AbstractList$Itr.next(AbstractList.java:420)
       at au.com.databaseapplications.jtest.session.PrincipalSessionBean.newYear(PrincipalSessionBean.java:355)
      


      The last line referred to in the exception above is the one indicated in the code.

      Any idea how one can do what seems to be simple?