非法尝试将非集合映射为

非法尝试将非集合映射为

本文介绍了“非法尝试将非集合映射为@ OneToMany,@ ManyToMany或@CollectionOfElements”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好Stackoverflow,

Good morning Stackoverflow,

我遇到的问题是它给了我错误:

I have the problem that it gives me the error:

您知道为什么吗?

@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 ,因此原因很明显,如果我们看一下明确指出:

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:

您使用了这是一个实现 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”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:22