在 JPA 中使用泛型 @OneToMany

2024-02-07

我有一个班级“分支”。我想在这里将其用作“Dictionary”和“Link”类。

@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通用没有任何好处。 我如何在这里使用泛型?


这不可能。

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 JPA 中使用泛型 @OneToMany 的相关文章

随机推荐