“非法尝试将非集合映射为@ OneToMany,@ ManyToMany或@CollectionOfElements” [英] “Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements”
问题描述
早上好Stackoverflow,
Good morning Stackoverflow,
我遇到的问题是它给了我错误:
I have the problem that it gives me the error:
无法创建sessionFactory
object.org.hibernate.AnnotationException:非法尝试将非
集合映射为@ OneToMany,@ ManyToMany或@CollectionOfElements:
nl.scalda .pasimo.model.employeemanagement.EducationTeam.coachGroups
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 集合映射文档明确指出:
The Exception is straightforward and says : Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements, so the cause is obvious here and if we take a look at the Hibernate Collection mapping documentation it clearly states that:
根据要求,必须将持久性集合值字段声明为接口类型(请参见示例7.2, 使用@OneToMany和@JoinColumn进行集合映射)。实际的接口可能是
java.util.Set
,java.util.Collection
,java .util.List
,java.util.Map
,java.util.SortedSet
,java.util.SortedMap
...
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>
接口的 class 。因此,您的实际映射不适用于 TreeSet
,您应该使用 Set< CoachGroup>
而不是 TreeSet< CoachGroup>
:
And you used TreeSet which is an implementation class for both Set<E>
and SortedSet<E>
interfaces. So your actual mapping won't work with TreeSet
, you should use a Set<CoachGroup>
instead of a TreeSet<CoachGroup>
:
private Set<CoachGroup> coachGroups = new HashSet<CoachGroup>();
这篇关于“非法尝试将非集合映射为@ OneToMany,@ ManyToMany或@CollectionOfElements”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!