I have a spring boot app, where I use h2, and a script to create a table as well as I also add values to it. I followed this tutorial: 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]
In the entity class, my property:
@Column(nullable = true, unique = false)
@JsonProperty
private int rateNum;
And then in the schema.sql
DROP TABLE IF EXISTS MOVIE;
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 );
And in data.sql:
-- noinspection SqlNoDataSourceInspectionForFile
INSERT INTO MOVIE (title, genre, rate, description, rateNum) 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);
My repository 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;
}
}
`
My repository class
`import com.example.movies.model.Movie;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MovieRepository extends JpaRepository<Movie, Long> {
}`
CodePudding user response:
Solution was here after all (It is sad that one has to spend so much time for small tweaks like this one): Why do I get a "Null value was assigned to a property of primitive type setter of" error message when using HibernateCriteriaBuilder in Grails
CodePudding user response:
Use the corresponding wrapper class Integer as primitives such as int cannot hold null values.
@Column(nullable = true, unique = false)
@JsonProperty
private Integer rateNum;