首页 > 解决方案 > 尝试在 Spring 中保存具有连接列的实体时,在休眠中获取非空约束违规

问题描述

我在尝试保存实体时收到非空约束违规,并且无法准确确定我的注释和类定义需要是什么才能使其工作。

我的数据库包含与评级具有一对多关系的电影。表和类定义如下:

电影:

   Column    |          Type          | Collation | Nullable | Default
-------------+------------------------+-----------+----------+---------
 movie_id    | character varying(15)  |           | not null |
 title       | character varying(250) |           | not null |
 year        | integer                |           |          |
 runningtime | integer                |           |          |
Indexes:
    "movie_pkey" PRIMARY KEY, btree (movie_id)
Referenced by:
    TABLE "rating" CONSTRAINT "rating_movie_id_fkey" FOREIGN KEY (movie_id) REFERENCES movie(movie_id)
@Entity
@Table(name = "movie")
public class Movie {
    @Id
    @Column(name = "movie_id")
    private String id;
    private String title;
    private Integer year;
    @Column(name = "runningtime")
    private Integer runningTime;

    @OneToMany(mappedBy="movie", cascade=CascadeType.ALL)
    private List<Rating> ratings;

    public Movie(String id, String title, Integer year, Integer runningTime) {
        this.id = id;
        this.title = title;
        this.year = year;
        this.runningTime = runningTime;
    }

    public Movie() {

    }

评分:

   Column   |            Type             | Collation | Nullable |                  Default
------------+-----------------------------+-----------+----------+-------------------------------------------
 rating_id  | integer                     |           | not null | nextval('rating_rating_id_seq'::regclass)
 movie_id   | character varying(15)       |           | not null |
 rating     | integer                     |           | not null |
 created_at | 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)
@Entity(name = "Rating")
@Table(name = "rating")
public class Rating {
    @Id
    @GeneratedValue(
            strategy = GenerationType.IDENTITY
    )
    @Column(name = "rating_id")
    private Long id;
    @Column(name = "rating")
    private Double ratingValue;
    @Column(name = "created_at", insertable = false)
    private LocalDateTime time;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "movie_id")
    private Movie movie;

    public Rating(String movieId, Double ratingValue) {
        this.ratingValue = ratingValue;
        this.time = LocalDateTime.now();
    }

    public Rating() {

    }

我的 RatingController 类具有以下映射来保存评分:

    @PostMapping
    public void addRating(@RequestBody Rating rating) {
        ratingRepository.save(rating);
    }

当我的 PostMapping 触发时,收到的 JSON 正文包括: rating: "Rating{id=null, movieId=0119217, ratingValue=9.0, time=null} 但是休眠告诉我: Failing row contains (4, null, 9, null).

我可以看到在保存发生之前生成了评级 ID,因为它在抛出的错误中确实有一个值 4,并且在 Postgres 中创建实体时,“时间”列是通过时间戳生成来处理的。但是,我不知道如何为movie_id 赋值。我之前在 Rating 中保留了一个私有字符串,但我意识到这与列的连接是多余的,因为编译它的唯一方法是添加updateable=false, insertable=false到 movieId 列注释,此时真的没有理由这样做在那里,因为这些注释会导致 spring 在保存时不更新该值。

有人知道如何配置它以便我可以保存新评级吗?

标签: javaspringpostgresqlhibernate

解决方案


正如svn建议的那样,我必须在保存之前设置 Movie 字段,所以我将 PostMapping 的参数更改为@RequestBody Map<String, String> jsonBody,调用并在保存之前movieRepository.findById(jsonBody.get("movieId")将返回的字段设置为评级。Movie如果有人知道比这更有效的方法,请告诉我,但这暂时有效!


推荐阅读