Search code examples
javahibernatepersistencespring-roo

WrongClassException when assigning entries on one side of many to many relationship


I'm using Spring Roo (and Hibernate) for a web application.

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

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?


Solution

  • By just looking at the syntax, try setting the value for the mappedBy property in the @ManyToMany annotation within the Course class.