早上好 Stackoverflow,
我的问题是它给了我错误:
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
你知道为什么吗?
@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);
}
}
最佳答案
异常很简单,说:非法尝试将非集合映射为@OneToMany、@ManyToMany 或@CollectionOfElements,所以原因在这里很明显,如果我们看看 Hibernate Collection mapping documentation它明确指出:
As a requirement persistent collection-valued fields must be declared as an interface type (see Example 7.2, “Collection mapping using @OneToMany and @JoinColumn”). The actual interface might be
java.util.Set
,java.util.Collection
,java.util.List
,java.util.Map
,java.util.SortedSet
,java.util.SortedMap
...
您使用了 TreeSet,它是 Set<E>
和 SortedSet<E>
接口(interface)的实现类。因此,您的实际映射不适用于 TreeSet
,您应该使用 Set<CoachGroup>
而不是 TreeSet<CoachGroup>
:
private Set<CoachGroup> coachGroups = new HashSet<CoachGroup>();
关于java - “Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44258541/