使用 Hibernate 和 JPA,我想映射一个包含其自身对象列表的类,例如:
public class Category{
private List<Category> subCategories = new ArrayList<Category>();
}
我正在尝试使用以下方法来做到这一点:
@OneToMany(fetch = FetchType.EAGER, mappedBy = "category")
但是当我尝试这个时,我收到以下错误:
Initial SessionFactory creation failed.org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: my.domain.name.Category.category in my.domain.name.Category.subCategories
最佳答案
试试这个:
public class Category {
@ManyToOne
private Category superCategory;
@OneToMany(mappedBy = "superCategory")
private List<Category> subCategories = new ArrayList<Category>();
}
您的示例中的问题是您的类中没有这样的属性category
。
关于java - 使用 Hibernate 映射 Java 类 : How can I map a list of its own objects inside the class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9982023/