2017-05-30 92 views
4

早安#1,「非法嘗試映射非集合作爲一個@OneToMany,@ManyToMany或@CollectionOfElements」

我有它給我的錯誤問題:

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); 
    } 
} 
+0

?:

因此您的實際映射不會TreeSet工作,你應該使用的Set<CoachGroup>代替TreeSet<CoachGroup>

回答

4

唯一的例外是直截了當地說:非法嘗試以非集合映射爲@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這是一個imple心理狀態兩個Set<E>SortedSet<E>接口類。你爲什麼要使用`TreeSet`而不是`Set`

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

您應該映射到接口而不是實現。這:

@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<>(); 
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<>(); 
0

您無法將收藏字段保存爲具體類。

得到這個,

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 or anything you like ("anything you like" means you will have to write an implementation of org.hibernate.usertype.UserCollectionType).

Chapter 7. Collection Mapping

您可以使用下面的代碼保存有序集合(敬請閱讀註釋):

@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<>(); 
相關問題