嵌套异常是 org.hibernate.AnnotationException:mappedBy 引用未知目标实体 属性

nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property

基于 this tutorial 我正在尝试使用复合键创建多对多关系 最终我得到 the following error

组合键本身具有以下结构:

@Embeddable
@Data
public class MovieRatingsKey implements Serializable {

    @Column(name = "movieid")
    private Movies movieid;

    @Column(name = "userid")
    private Usrs userid;
}

Class 电影:

@Entity
@Data
public class Movies {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer movieid;

    /*not related to the question*/

    @OneToMany(mappedBy = "movieid", fetch = FetchType.LAZY)
    private Set<Ratings> rates = new HashSet<>();
}

用户:

@Entity
@Data
public class Usrs {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer userid;

    @OneToMany(mappedBy = "userid", fetch = FetchType.LAZY)
    private Set<Ratings> rates = new HashSet<>();
}

关联table:

@Entity
@Data
public class Ratings {
    @EmbeddedId
    private MovieRatingsKey id;
    
    /*not related to the question*/

    @ManyToOne
    @MapsId("movieid")
    @JoinColumn(name = "movieid")
    Movies movie;

    @ManyToOne
    @MapsId("userid")
    @JoinColumn(name = "userid")
    Usrs user;
}

在您的 Usrs 中,您在 mappedBy 中使用了 userid 应该是 user,如下所示。

@Entity
@Data
public class Usrs {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer userid;

    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
    private Set<Ratings> rates = new HashSet<>();
}