Use Generic in JPA @OneToMany

邮差的信 提交于 2021-01-29 00:40:36

问题


I have a class "Branch". Either classes "Dictionary" and "Link" which I want to use as < T> here.

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

It throws a Runtime Exception:

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

If I use targetEntity=SomeEntity.class there is no benefits of generic. How can I use generics here?


回答1:


This is not possible.

The issue here is more in Branch<T> declaration than in List<T> entities. When JPA provider reads the record of Branch from database, how would it know what T is?

You would need to provide an interface/superclass instead of T, be it only an empty marker one if the differences between subclasses are large as in your case.



来源:https://stackoverflow.com/questions/30306897/use-generic-in-jpa-onetomany

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!