“Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements”

Viewed 69420

Good morning Stackoverflow,

I have the problem that it gives me the error:

Failed to create sessionFactory object.org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: nl.scalda.pasimo.model.employeemanagement.EducationTeam.coachGroups

Do you know why?

@OneToMany(cascade=CascadeType.ALL, targetEntity=CoachGroup.class)
@JoinColumn(name="id")
private TreeSet<CoachGroup> coachGroups = new TreeSet<>();
private SessionFactory factory;

private void initialiseFactory() {
    try {
        factory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}
5 Answers

Another possible reasons for this exception to occur is using a non-collection object for @ManyToMany and@OneToMany mappings Or using collection object for @ManyToOne and @OneToOne mappings. All examples below are incorrect.

INCORRECT

 @ManyToMany
 private User user;

 @ManyToOne
 private User user;

 @OneToOne
 private List<User> users;

 @ManyToOne
 private List<User> users;
Related