简体   繁体   English

SpringBoot JPA MappedBy通过引用未知目标实体属性

[英]SpringBoot JPA MappedBy reference an unknown target entity property

The error: hibernate.AnnotationException: mappedBy reference an unknown target entity property 错误:hibernate.AnnotationException:mappedBy引用未知的目标实体属性

I know why the error is here but the longer i stare at it the more I cannot find it :). 我知道为什么会出现错误,但是我盯着它看的时间越长,我就越找不到它:)。 I just need another persons perspective. 我只需要另一个人的角度。

Here are the MySQL tables: 这是MySQL表: 在此处输入图片说明

Then I have entities for User, Company and Transaction. 然后,我有用户,公司和交易的实体。

The relationships are shown here: 关系如下所示:

Transaction: 交易:

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

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

Company 公司

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

User 用户

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

Here is the full error: 这是完整的错误:

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

So the error is to do with the relationship but I cannot spot my mistake. 因此,错误与关系有关,但我无法发现我的错误。

Thanks :) 谢谢 :)

Maybe you try mapped by field "user". 也许您尝试按“用户”字段进行映射。 Probably you may see asked this question in there Understanding mappedBy annotation in Hibernate 可能您会在“ 了解Hibernate中的mappingBy批注”中看到这个问题。

Company 公司

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

User 用户

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

The mappedBy is referring to the name of the field that is connected to. mappingBy指的是连接到的字段的名称。 The link that @Aleksandr Zorin provided has more details. @Aleksandr Zorin提供的链接具有更多详细信息。 I recommand you to take a look :) 我建议你看看:)

It should be 它应该是

Company 公司

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

and

User 用户

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

In mappedBy property you are specifying field in referenced table by which it should be mapped. mapledBy属性中,您在引用表中指定字段,应以此字段进行映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Hibernate“ mappedBy引用未知目标实体属性” - “mappedBy reference an unknown target entity property” using Hibernate org.hibernate.AnnotationException:mappedBy引用未知的目标实体属性。 发生错误:java.lang.NullPointerException - org.hibernate.AnnotationException: mappedBy reference an unknown target entity property. There is an error: java.lang.NullPointerException springboot + JPA + MySql + Entity 表生成大写 - springboot + JPA + MySql + Entity table generated uppercase Springboot Jpa-如何将实体更新到mysql数据库 - Springboot Jpa - How to update entity to mysql database SQL插入后,不会从数据库获取“ mappedBy”属性的新条目 - Spring Data + JPA > New entries of “mappedBy” property are not fetched from DB after SQL INSERT 休眠的OneToOne关系中未知的mappingBy - Unknown mappedBy in hibernate OneToOne relationship java.sql.SQLSyntaxErrorException:未知列.JPA实体问题? - java.sql.SQLSyntaxErrorException: Unknown column .JPA Entity Issue? 如何在 SpringBoot 中使用具有默认属性值的枚举列设置实体 - How to set an entity with enum column with default property value in SpringBoot springboot存储库jpa ClassCastException - springboot repository jpa ClassCastException 批量更新springboot jpa - bulk update springboot jpa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM