【发布时间】:2018-01-09 12:38:48
【问题描述】:
Hibernate 有可能有这样的东西吗:
public interface FooList extends List<Bar> {}
然后使用@OneToMany将其映射到一个类中:
public class Baz {
@OneToMany
FooList fooList;
}
当我尝试这样做时,我得到:
非法尝试将非集合映射为@OneToMany、@ManyToMany 或@CollectionOfElements
【问题讨论】:
-
为什么需要扩展
List? -
你试过简单的
List<Bar> fooList;吗? -
因为在 Java 中泛型在编译时被删除,但有时有一个精确的类型来推断——在运行时——确实是一个 Bar 的列表,而不仅仅是一个 List (例如,我可以说
myMethod(FooList.class),但我不能写myMethod(List<Bar>.class))。这只是为了方便我可能会更改我的代码,但如果它不可能使用休眠。