[英]@OneToMany has an error: mappedBy reference an unknown target entity property
My model consists of different types of Budgets.我的 model 包含不同类型的预算。 These Budgets have different Attributes.
这些预算具有不同的属性。 I want to make a OneToMany Relationship between these different types of Budgets.
我想在这些不同类型的预算之间建立一对多关系。 So lets say I have BudgetLevel1 that has many BudgetLevel2, which again has many BudgetLevel3.
所以假设我有 BudgetLevel1,它有很多 BudgetLevel2,它又有很多 BudgetLevel3。 All these BudgetLevels extend the Class Budget
所有这些 BudgetLevels 都扩展了 Class Budget
My Budget Class我的预算 Class
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Budget {
@Id
private Long id;
private String name;
private BudgetTyp budgetTyp;
private Double amount;
BudgetLevel1 Class预算级别 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;
BudgetLevel2 Class预算级别 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 would look similar Budgetlevel3 看起来很相似
this is the error I get这是我得到的错误
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;
org.springframework.beans.factory.BeanCreationException:创建名为“entityManagerFactory”的 bean 在 class 路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 中定义时出错:调用 init 方法失败; 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.hibernate.AnnotationException:mappedBy 引用未知目标实体属性:com.bm.ent.geschaeftsfeld.marke.BudgetLevel2.budgetlevel1 in com.bm.ent.geschaeftsfeld.BudegetLevel1.budgetframework.beg.springframeworks.beg.springframelevel2 .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
org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.5.4.jar:2.5.4] 原因:org.hibernate.AnnotationException:mappedBy 引用未知目标实体属性:com.bm.ent.geschaeftsfeld.marke.BudgetLevel2.budgetlevel1 in com.bm.ent.geschaeftsfeld.BudegetLevel1.budgetlevel2
mappedBy
works by referencing the actual field names. mappedBy
通过引用实际的字段名称来工作。 As you write mappedBy="budgetlevel2"
, you tell hibernate to look for a field named budgetlevel2
, which you don't have.当您编写
mappedBy="budgetlevel2"
时,您告诉 hibernate 查找名为budgetlevel2
的字段,但您没有。
You need to reference the field name directly and to make sure that you have hibernate look in the right entity, also specify the targetEntity
like this:您需要直接引用字段名称并确保您有 hibernate 查看正确的实体,还需要像这样指定
targetEntity
:
@OneToMany(
mappedBy="budgets",
cascade=CascadeType.ALL,
orphanRemoval=true,
targetEntity = BudgetLevel2.class
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.