springboot jpa mappedby引用未知的目标实体属性

4ioopgfo  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(336)

错误:hibernate.annotationexception:mappedby引用未知的目标实体属性
我知道错误为什么在这里,但我盯着它看的时间越长,我就越找不到它:)。我只需要另一个人的视角。
以下是mysql表:

然后我有用户、公司和交易的实体。
关系如下所示:
交易:

@ManyToOne
    @JoinColumn(name = "userId")
    User user;

    @ManyToOne
    @JoinColumn(name = "companyId")
    Company company;

公司

@JsonIgnore
    @OneToMany(mappedBy = "transaction")
    List<Transaction> transactions;

用户

@JsonIgnore
    @OneToMany(mappedBy = "transaction")
    List<Transaction> transactions;

以下是完整错误:

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: top100.models.Transaction.transaction in top100.models.User.transactions

所以这个错误是和关系有关的,但我不能发现我的错误。
谢谢:)

vbopmzt1

vbopmzt11#

也许您可以尝试按字段“user”Map。也许你会在那里看到在hibernate中理解mappedby注解的问题

nzk0hqpo

nzk0hqpo2#

应该是的
公司

@JsonIgnore
@OneToMany(mappedBy = "company")
List<Transaction> transactions;

以及
用户

@JsonIgnore
@OneToMany(mappedBy = "user")
List<Transaction> transactions;

在mappedby属性中,您指定引用表中的字段,该字段应被Map。

tnkciper

tnkciper3#

公司

@JsonIgnore
@OneToMany(mappedBy = "company")
List<Transaction> transactions;

用户

@JsonIgnore
@OneToMany(mappedBy = "user")
List<Transaction> transactions;

mappedby是指连接到的字段的名称。@aleksandrzorin提供的链接有更多细节。我建议你看看:)

相关问题