注释异常:mappedBy 引用未知目标实体 属性

Annotation Exception: mappedBy reference an unknown target entity property

我是 Hibernate 和 JPA 的新手,我正在尝试连接电影、评分者和评级的数据库。我一直收到错误 Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.stephenalexander.projects.movierecommender.rating.Rating.movie in com.stephenalexander.projects.movierecommender.movie.Movie.ratings。每个 Movie 对象和每个 Rater 对象都与 Rating 具有 OneToMany 关系。这是我写的东西:


评分:

package com.stephenalexander.projects.movierecommender.rating;

import com.stephenalexander.projects.movierecommender.movie.Movie;
import com.stephenalexander.projects.movierecommender.rater.Rater;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Objects;


@Entity
@Table(name="rating")
public class Rating {
    @Column(name = "rating_id")
    private Long ratingId;
    @Column(name = "rating")
    private Double ratingValue;
    private LocalDateTime time;

    @ManyToOne
    @JoinColumn(name = "rater_id", nullable = false)
    private Rater rater;

    @ManyToOne
    @JoinColumn(name = "movie_id", nullable = false)
    private Movie movie;

电影:

package com.stephenalexander.projects.movierecommender.movie;

import com.stephenalexander.projects.movierecommender.rating.Rating;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "movie")
public class Movie {
    @Id
    @GeneratedValue(
            strategy = GenerationType.AUTO
    )

    @Column(name = "movie_id")
    private Integer movieId;
    private String title;
    private int year;
    @Column(name = "posterurl")
    private String posterUrl;
    @Column(name = "runningtime")
    private int runningTime;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "movie")
    List<Rating> ratings;

评分者:

package com.stephenalexander.projects.movierecommender.rater;

import com.stephenalexander.projects.movierecommender.rating.Rating;

import javax.persistence.*;
import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "rater")
public class Rater {
    @Id
    @GeneratedValue(
            strategy = GenerationType.AUTO
    )
    @Column(name = "rater_id")
    private Long raterId;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "rater")
    private List<Rating> ratings;

我看过其他一些帖子,这个错误似乎最常见的原因是 mappedBy 注释引用了所有者 class 中实例变量名称以外的其他内容。我相信我做对了,因为 Rating class 有一个名为 rater 的变量和一个名为 movie 的变量。我不确定还有什么可能导致错误。非常感谢阅读!

编辑:在此处的答案中添加我的评论以阐明我认为这些注释是如何工作的:

据我理解,List<Rating> ratingsmappedBy:"movie",所以Rating中的movie对象通过其注解会告诉你关系在哪一列建立在,它是电影的主键和评分的外键,movie_idrater_id 也一样。这不是整个事情的运作方式吗?

编辑 2:我的另一个想法可能是一种误解...如果我已经在我的 psql 数据库中设置了关系,那么这些符号是否不是必需的?这是多余的吗?我认为这些的目的是让我更轻松地浏览 java 中的查询。在我不断尝试编写查询和 运行 导致错误导致我相信我的代码不知道这些表是相关的之后,我被引导到这些定义关系的符号。以下是它们在 PostgreSQL 中的全部设置方式:

-----------+-----------------------------+-----------+----------+-------------------------------------------
 rating_id | integer                     |           | not null | nextval('rating_rating_id_seq'::regclass)
 rater_id  | integer                     |           | not null |
 movie_id  | integer                     |           | not null |
 rating    | smallint                    |           |          |
 time      | timestamp without time zone |           |          |
Indexes:
    "rating_pkey" PRIMARY KEY, btree (rating_id)
Foreign-key constraints:
    "rating_movie_id_fkey" FOREIGN KEY (movie_id) REFERENCES movie(movie_id) ON UPDATE CASCADE
    "rating_rater_id_fkey" FOREIGN KEY (rater_id) REFERENCES rater(rater_id) ON UPDATE CASCADE
                Table "public.rater"
  Column  |  Type   | Collation | Nullable | Default
----------+---------+-----------+----------+---------
 rater_id | integer |           | not null |
Indexes:
    "rater_pkey" PRIMARY KEY, btree (rater_id)
Referenced by:
    TABLE "rating" CONSTRAINT "rating_rater_id_fkey" FOREIGN KEY (rater_id) REFERENCES rater(rater_id) ON UPDATE CASCADE
                         Table "public.movie"
   Column    |          Type          | Collation | Nullable | Default
-------------+------------------------+-----------+----------+---------
 movie_id    | integer                |           | not null |
 title       | character varying(100) |           | not null |
 year        | smallint               |           | not null |
 runningtime | smallint               |           | not null |
 posterurl   | character varying(350) |           |          |
Indexes:
    "movie_pkey" PRIMARY KEY, btree (movie_id)
Referenced by:
    TABLE "movies_genres" CONSTRAINT "fk_movie" FOREIGN KEY (movie_id) REFERENCES movie(movie_id)
    TABLE "movies_directors" CONSTRAINT "fk_movie" FOREIGN KEY (movie_id) REFERENCES movie(movie_id)
    TABLE "movies_countries" CONSTRAINT "fk_movie" FOREIGN KEY (movie_id) REFERENCES movie(movie_id)
    TABLE "rating" CONSTRAINT "rating_movie_id_fkey" FOREIGN KEY (movie_id) REFERENCES movie(movie_id) ON UPDATE CASCADE
@Column(name = "movie_id")
private Integer movieId;

@ManyToOne
@JoinColumn(name = "movie_id", nullable = false)
private Movie movie;

你在评级和电影中有一个与 movie_id 相同的列。因此有冲突。

改变其中之一。

缺少的是 @OneToManytargetEntity = Rating.class 参数。所以不同类内的函数注解如下:

电影

@OneToMany(fetch = FetchType.LAZY, targetEntity = Rating.class, mappedBy = "movie")
private List<Rating> ratings

评价者

@OneToMany(fetch = FetchType.LAZY, targetEntity = Rating.class, mappedBy = "rater")
private List<Rating> ratings

评分

@ManyToOne
@JoinColumn(name = "rater_id", nullable = false)
private Rater rater;

@ManyToOne
@JoinColumn(name = "movie_id", nullable = false)
private Movie movie;

感谢所有看过这篇文章的人!