在JPA @OneToMany中使用Generic

时间:2015-05-18 15:14:18

标签: java generics jpa

我有一个班级"分支"。课程和#34;字典"和"链接"我想用它作为< T>这里。

@Entity
public class Branch<T> {
   ...
   @OneToMany(mappedBy = "branch", orphanRemoval = true, cascade = CascadeType.ALL)
   private List<T> entities = new ArrayList<>();
}

它会抛出一个运行时异常:

org.hibernate.AnnotationException: Property by.ipps.accounting.model.Branch.branches has an unbound type and no explicit target entity. Resolve this Generic usage issue or set an explicit target attribute (eg @OneToMany(target=) or use an explicit @Type

如果我使用targetEntity=SomeEntity.class,则没有通用的好处。 我怎样才能在这里使用泛型?

1 个答案:

答案 0 :(得分:0)

这是不可能的。

Branch<T>声明中的问题多于List<T> entities中的问题。当JPA提供程序从数据库中读取Branch的记录时,它如何知道T是什么?

您需要提供一个接口/超类而不是T,如果子类之间的差异很大,那么它只是一个空标记。