To further clarify, the following two SQL queries do what I want done:
"delete from STUDENT_SUBJECT;" and
"delete from STUDENT_CLASS;"
The second one is not mentioned in the post, but if I can get one to work, so will the other.
There is a method that removes any number of stidents (comma separated) from a subject and also from the class for a subject. Putting the code for this in a loop for all subjects does not work with the same exception as above.
This should be possible, and preferably without the looping - more something like the native delete query, which is not implemented in the EJB3/JBoss version I use (4.0.3)
Maybe I should create explicit classes for the relationship tables and one to many relationships. I think I can get that to work. What a hassle. It will mean a lot of re-writing.
I think you get the ConcurrentModificationException because you remove objects from the list while you're iterating over it.
If you go through the list with a simple
for (int i = 0; i < subjects.size(); i++)
maybe it will work?
Thanks very much. That was it. However, before the code posted above I had a for(Subject s : subjects). That threw the same exceptions. This time I got a Set of subjects and turned it into an Object array and for looped through that - no problem. It didn't take long to execute, either.
Don't you have a relationship on EJB level so you can do student.getSubjects() instead of going through the trouble of doing a query? Looks awful complicated to me :)
Yes, I have. Then I did a subjects.clear() No exceptions or anything, but the student-subject relationship was still there in the relationship table in MySQL.
I think I may even have called flush(). I did call subjects.size() before clearing.