gpt4 book ai didi

java - 在 JPA 中使用泛型 @OneToMany

转载 作者:太空宇宙 更新时间:2023-11-04 13:50:44 25 4
gpt4 key购买 nike

我有一个“Branch”类(class)。我想在此处用作“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是什么?是?

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

关于java - 在 JPA 中使用泛型 @OneToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30306897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com