java - 尝试在 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 在保存时不更新该值。
有人知道如何配置它以便我可以保存新评级吗?
解决方案
正如svn建议的那样,我必须在保存之前设置 Movie 字段,所以我将 PostMapping 的参数更改为@RequestBody Map<String, String> jsonBody
,调用并在保存之前movieRepository.findById(jsonBody.get("movieId")
将返回的字段设置为评级。Movie
如果有人知道比这更有效的方法,请告诉我,但这暂时有效!
推荐阅读
- javascript - 需要在 JS 类中
- mysql - 连接时的 MySQL 索引使用情况
- javascript - Odoo 11 异常(服务器应用程序错误 "code":100,"message":"Odoo Session Expired","data")
- python - Pandas multiindex:获取没有重复的级别值
- machine-learning - 机器活动的时间序列聚类
- php - mysqli_num_rows 不工作?
- azure - Raspberry Pi 3 B+ 设置问题:“错误:'TLSv1_method' 已弃用”
- r - 使用 as.factor 时在 ggplot2 中重新排序条形图输出
- javascript - window.onunload 事件不会在 AngularJS 控制器中触发
- c++-winrt - 如何在 winrt::impl::blocking_suspend 中解决断言 WINRT_ASSERT(!is_sta())