首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sping启动AnnotationException - mappedBy引用未知的目标实体属性

Sping启动AnnotationException - mappedBy引用未知的目标实体属性
EN

Stack Overflow用户
提问于 2021-11-12 11:22:30
回答 1查看 63关注 0票数 0

我正在尝试理解实体关系在Spring Boot中是如何工作的。为了理解它们,我尝试实现以下数据库模式

到目前为止,我已经以这种方式实现了实体

用户

代码语言:javascript
复制
@Entity // This tells Hibernate to make a table out of this class
public class User {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;

  private String name;

  private String email;

  private String passwordDigest;

  @OneToMany(mappedBy = "followerId")
  Set<User> followers;

  @OneToMany(mappedBy = "followedId")
  Set<User> follows;

  @OneToMany(mappedBy = "user")
  Set<Tweet> tweets;

  @Temporal(TemporalType.DATE)
  private Date createdAt;
  
  @Temporal(TemporalType.DATE)
  private Date updatedAt;

  // constructors, getters and setters

}

关系

代码语言:javascript
复制
@Entity
@Table(indexes = @Index(columnList = "follower_id, followed_id"))
class Relationship {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "user_id")
    User followerId;

    @ManyToOne
    @JoinColumn(name = "user_id")
    User followedId;

    @Temporal(TemporalType.DATE)
    Date createdAt;
    
    @Temporal(TemporalType.DATE)
    Date updatedAt;

    // constructors, getters and setters

}

推文

代码语言:javascript
复制
@Entity
public class Tweet {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    String content;

    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    User user;

    // constructors, getters and setters
}

现在,在运行应用程序时,我得到以下错误

代码语言:javascript
复制
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.example.demo.entity.User.followerId in com.example.demo.entity.User.followers

这对我来说很奇怪因为我确定

代码语言:javascript
复制
@OneToMany(mappedBy = "followerId")
Set<User> followers;

在用户实体中匹配关系实体中followerId的属性

代码语言:javascript
复制
@ManyToOne
@JoinColumn(name = "user_id")
User followerId;

我错过了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-11-12 11:44:40

通过更改修复

代码语言:javascript
复制
@OneToMany(mappedBy = "followerId")
Set<User> followers;

@OneToMany(mappedBy = "followedId")
Set<User> follows;

代码语言:javascript
复制
@OneToMany(mappedBy = "followerId")
Set<Relationship> followers;

@OneToMany(mappedBy = "followedId")
Set<Relationship> follows;

在用户实体中,因为一对多关系以关系实体为目标

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69942154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档