Int 属性 保持为空(spring 启动/休眠)

Int property stays null (spring boot / hibernate)

我有一个 spring 引导应用程序,我在其中使用 h2 和一个脚本来创建 table 以及我还向它添加值。 我遵循了本教程: 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]

在实体class中,我的属性:

@Column(nullable = true, unique = false)
@JsonProperty
private int rateNum;

然后在schema.sql

如果有电影则删除 TABLE;

创建TABLE电影( id INT AUTO_INCREMENT 主键, 标题 VARCHAR(45) 不为空, 类型 VARCHAR(45) NOT NULL, 率双不为空, 描述 VARCHAR(1000), rateNum INT 不为空 );

并且在 data.sql 中:

-- noinspection SqlNoDataSourceInspectionForFile

INSERT INTO MOVIE(标题、流派、费率、描述、费率编号)VALUES ('Early Man','Action/Adventure, Animated, Comedy',6.4,'Set at the dawn of time, when prehistoric creatures and woolly mammoths roamed the earth, Early Man tells the story of Dug, along with sidekick Hognob as they unite his tribe against a mighty enemy Lord Nooth and his Bronze Age City to save their home.',10);

我的存储库 class:

`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;
    }
}

`

我的存储库class

`import com.example.movies.model.Movie;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MovieRepository extends JpaRepository<Movie, Long> {
}`

毕竟解决方案在这里(令人遗憾的是,一个人不得不花这么多时间进行像这样的小调整): Why do I get a "Null value was assigned to a property of primitive type setter of" error message when using HibernateCriteriaBuilder in Grails

使用相应的包装器 class Integer 因为 int 等基元不能保存空值。

@Column(nullable = true, unique = false)
@JsonProperty
private Integer rateNum;