我在 Set 上使用了 @ManyToMany
注释,效果很好。
@ManyToMany
@JoinTable(name="stud_course_map",joinColumns={@JoinColumn(name="stud_id ")}, inverseJoinColumns={@JoinColumn(name="course_id")} )
public Set<Course> getEnrolledCourses() {
return enrolledCourses;
}
当我使用 ArrayList
而不是 Set
时,它会抛出异常:
org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.kaushik.winnersoft.data.Student.enrolledCourses
不支持ArrayList
吗? @ManyToMany
注解支持哪些所有集合?
最佳答案
支持的接口(interface)有:
java.util.Set
java.util.List
java.util.Collection
您收到异常是因为您使用了这些接口(interface)之一的具体实现。始终使用上面给出的接口(interface)定义 @ManyToMany
或 @OneToMany
就可以了。
关于java - Hibernate @ManyToMany 注解支持哪些集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44157641/