You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot启动报AnnotationException:mappedBy引用未知目标实体属性问题

问题描述

我正在学习Spring Boot中的实体关系映射规则,为加深理解尝试实现如下数据库架构:
数据库架构图
目前我编写的三个实体类代码如下:

User

@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

}

Relationship

@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

}

Tweets

@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
}

启动应用时抛出如下错误信息:

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

我已确认User实体中配置的@OneToMany(mappedBy = "followerId")和Relationship实体中的followerId属性是匹配的,但仍然触发该报错,请问我遗漏了哪些配置?


解决方案

存在3处核心配置错误,对应修正方案如下:

  1. User类关联目标实体类型错误
    你在@OneToMany注解中把关联目标设为User,但followerId属性实际存在于Relationship实体中,这是报错的直接原因。修正关联字段的泛型类型:
// 原代码
// @OneToMany(mappedBy = "followerId")
// Set<User> followers;
// @OneToMany(mappedBy = "followedId")
// Set<User> follows;

// 修正后代码
@OneToMany(mappedBy = "followerId")
Set<Relationship> followers;

@OneToMany(mappedBy = "followedId")
Set<Relationship> follows;
  1. Relationship类外键字段冲突
    两个关联字段都指定了同一个@JoinColumn(name = "user_id"),会导致表字段重复,和你定义的索引规则也不匹配,修正为:
@ManyToOne
@JoinColumn(name = "follower_id")
User followerId;

@ManyToOne
@JoinColumn(name = "followed_id")
User followedId;
  1. Relationship类访问权限问题
    当前Relationship类没有加public修饰符,如果和User类不在同一个包下,Hibernate无法扫描到该实体,补充public修饰符即可。

如果不需要单独维护Relationship实体的额外字段,也可以直接在User类中配置多对多关联省略中间实体声明:

@ManyToMany
@JoinTable(
  name = "relationship",
  joinColumns = @JoinColumn(name = "followed_id"),
  inverseJoinColumns = @JoinColumn(name = "follower_id")
)
Set<User> followers;

@ManyToMany
@JoinTable(
  name = "relationship",
  joinColumns = @JoinColumn(name = "follower_id"),
  inverseJoinColumns = @JoinColumn(name = "followed_id")
)
Set<User> follows;

内容的提问来源于stack exchange,提问作者unnikked

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 23:36:04