“非法尝试将非集合映射为@OneToMany,@ ManyToMany或@CollectionOfElements”

时间:2017-05-30 09:31:46

标签: java hibernate

早上好Stackoverflow,

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

  

无法创建sessionFactory   object.org.hibernate.AnnotationException:非法尝试映射非   收集为@OneToMany,@ ManyToMany或@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);
    }
}

5 个答案:

答案 0 :(得分:17)

Exception很简单,并说:非法尝试将非集合映射为@OneToMany,@ ManyToMany或@CollectionOfElements ,所以原因很明显,如果我们看一下{ {3}}它明确指出:

  

作为要求,必须将持久集合值字段声明为接口类型(请参见例7.2“使用@OneToMany和@JoinColumn进行集合映射”)。实际界面可能是java.util.Setjava.util.Collectionjava.util.Listjava.util.Mapjava.util.SortedSetjava.util.SortedMap ......

您使用Hibernate Collection mapping documentation这是Set<E>SortedSet<E>接口的实现。因此,您的实际地图无法与TreeSet一起使用,您应该使用Set<CoachGroup>代替TreeSet<CoachGroup>

private Set<CoachGroup> coachGroups = new HashSet<CoachGroup>();

答案 1 :(得分:1)

您应该映射到接口而不是实现。这样:

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

应该是(也替换了TreeSet,因为HashSet就足够了):

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

答案 2 :(得分:0)

不允许在实体字段声明中使用具体实现。您可以使用以下其中一项:

  • 的java.util.List
  • java.util.Set中
  • java.util.Collection中

所以在你的情况下,它必须是:

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

答案 3 :(得分:0)

您无法将收藏字段另存为具体类。

得到了这个,

  

作为要求,必须声明持久的集合值字段   作为接口类型(参见例7.2,“使用的集合映射”)   @OneToMany和@JoinColumn“)。实际的界面可能是   java.util.Set,java.util.Collection,java.util.List,java.util.Map,   java.util.SortedSet,java.util.SortedMap或任何你喜欢的东西   (&#34;你喜欢的任何东西&#34;意味着你必须编写一个实现   org.hibernate.usertype.UserCollectionType)。

来自Chapter 7. Collection Mapping

您可以使用以下代码保存已排序的集合(KINDLY READ THE COMMENTS):

@OneToMany(cascade=CascadeType.ALL) //Removed targetEntity, as you are already using generics.
@JoinColumn(name="team_id") // Use this name as to show the presence of foreign key of EducationTeam in CoachGroup.
@SortNatural // Make sure that your CoachGroup Entity has implemented Comparable<CoachGroup> interface which wii be used while sorting.
private SortedSet<CoachGroup> coachGroups = new TreeSet<>();

答案 4 :(得分:0)

发生此异常的另一个可能原因是将非集合对象用于 @ManyToMany@OneToMany映射,或将集合对象用于@ManyToOne@OneToOne映射。以下所有示例均不正确。

错误

 @ManyToMany
 private User user;

 @ManyToOne
 private User user;

 @OneToOne
 private List<User> users;

 @ManyToOne
 private List<User> users;