I've got two entities which have a MANY-TO-MANY relationship:

Code:
public class Student{

    @ManyToMany(cascade = CascadeType.ALL,mappedBy="students")
    private Set<Course> courses = new HashSet<Course>();

}

public class Course{

    @ManyToMany(cascade = CascadeType.ALL)
    private Set<Student> students = new HashSet<Student>();
}
In the scaffolded GUI I can assign multiple courses to students, but when I try to assign students to courses I get this error:

org.hibernate.WrongClassException: Object with id: null was not of the specified subclass: com.company.Student(class of the given object did not match class of persistent copy); nested exception is javax.persistence.PersistenceException: org.hibernate.WrongClassException: Object with id: null was not of the specified subclass: com.company.Student(class of the given object did not match class of persistent copy)
What may be the cause of the problem?