@OneToMany has an error: mappedBy reference an unknown target entity property

@OneToMany has an error: mappedBy reference an unknown target entity property

我的模型包含不同类型的预算。这些预算具有不同的属性。 我想在这些不同类型的预算之间建立一对多关系。 所以假设我有 BudgetLevel1,它有很多 BudgetLevel2,它又有很多 BudgetLevel3。 所有这些 BudgetLevels 都扩展了 Class Budget

我的预算Class

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Budget {

@Id
private Long id;
private String name;
private BudgetTyp budgetTyp;
private Double amount;

预算水平 1 Class

@Entity(name= "BudgetLevel1 ")
public class BudgetLevel1 extends Budget {

@OneToMany(
            **mappedBy="attBudgetLevel2 ",**
            cascade=CascadeType.ALL,
            orphanRemoval=true
            )
private List<BudgetLevel2> budgets= new ArrayList<>();
private String att1;

预算水平 2 Class

@Entity(name= "BudgetLevel2")
public class BudgetLevel2 extends Budget {

@OneToMany(
            mappedBy="budgetlevel2",
            cascade=CascadeType.ALL,
            orphanRemoval=true
            )
private List<BudgetLevel3> budgets= new ArrayList<>();

**@ManyToOne( fetch=FetchType.LAZY,
        cascade=CascadeType.PERSIST)
@JoinColumn(name="budget_id")
Private BudgetLevel2 attBudgetLevel2;**

private String att2;

Budgetlevel3 看起来很相似

这是我得到的错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.bm.ent.geschaeftsfeld.marke.BudgetLevel2.budgetlevel1 in com.bm.ent.geschaeftsfeld.BudegetLevel1.budgetlevel2 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.9.jar:5.3.9]

org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.5.4.jar:2.5.4] Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.bm.ent.geschaeftsfeld.marke.BudgetLevel2.budgetlevel1 in com.bm.ent.geschaeftsfeld.BudegetLevel1.budgetlevel2

mappedBy 通过引用实际字段名称来工作。当你写 mappedBy="budgetlevel2" 时,你告诉 hibernate 去寻找一个你没有的名为 budgetlevel2 的字段。

您需要直接引用字段名称并确保您在正确的实体中有休眠外观,还需要像这样指定 targetEntity

@OneToMany(
            mappedBy="budgets",
            cascade=CascadeType.ALL,
            orphanRemoval=true,
            targetEntity = BudgetLevel2.class
            )