我有一個 spring boot 應用程式,我在其中使用 h2,以及一個創建表的腳本以及我還向它添加值。我遵循了本教程: https ://howtodoinjava.com/spring-boot2/h2-database-example/
java.lang.IllegalArgumentException: Can not set int field com.example.movies.model.Movie.rateNum to null value at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:na] at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[na:na]
在物體類中,我的屬性:
@Column(nullable = true, unique = false)
@JsonProperty
private int rateNum;
然后在 schema.sql
如果存在電影,則洗掉表;
CREATE TABLE MOVIE(id INT AUTO_INCREMENT PRIMARY KEY,title VARCHAR(45) NOT NULL,genre VARCHAR(45) NOT NULL,rate DOUBLE NOT NULL,description VARCHAR(1000),rateNum INT NOT NULL);
在 data.sql 中:
-- noinspection SqlNoDataSourceInspectionForFile
INSERT INTO MOVIE (title,genre, rate, description, rateNum) VALUES ('Early Man','Action/Adventure, Animated, Comedy',6.4,'設定在時間的黎明,史前生物和猛犸象在地球上漫游, Early Man 講述了 Dug 和助手 Hognob 聯合他的部落對抗強大的敵人 Nooth 勛爵和他的青銅時代城市以拯救他們的家園的故事。',10);
我的存盤庫類:
`package com.example.movies.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.persistence.*;
import java.util.Objects;
//https://howtodoinjava.com/spring-boot2/h2-database-example/
@Entity
@Table(name = "MOVIE")
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonProperty
private long id;
@Column(nullable = false, unique = true)
@JsonProperty
private String title;
@Column(nullable = false, unique = false)
@JsonProperty
private String genre;
@Column(nullable = false, unique = false)
@JsonProperty
private double rate;
@Column(nullable = true, unique = false)
@JsonProperty
private String description;
@Column(nullable = true, unique = false)
@JsonProperty
private int rateNum;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public int getRateNum() {
return rateNum;
}
public void setRateNum(int rateNum) {
this.rateNum = rateNum;
}
}
`
我的存盤庫類
`import com.example.movies.model.Movie;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MovieRepository extends JpaRepository<Movie, Long> {
}`
uj5u.com熱心網友回復:
使用相應的包裝類Integer,因為 int 等原語不能保存空值。
@Column(nullable = true, unique = false)
@JsonProperty
private Integer rateNum;
uj5u.com熱心網友回復:
畢竟解決方案就在這里(很遺憾,人們不得不花這么多時間進行像這樣的小調整): 為什么我在使用 HibernateCriteriaBuilder 時收到“Null value was assigned to an property of original type setter of”錯誤訊息圣杯
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/463974.html
上一篇:MappingException物體映射中的重復列:models.Book列:author_id(應使用insert="false"update="false"
