我有一个由MathematicsAnswer引用的实体math。如果对math执行post请求,我将得到MathsAnswer上的字段不能为空的异常。但我确实在球场上跳伞了。我需要解决这个问题。java.sql.SQLIntegrityConstraintViolationException: Column 'question_id' cannot be null
sql模式:
CREATE TABLE MATHEMATICS(
id integer not null auto_increment,
year date not null,
question_no int not null,
question varchar(128) default null,
primary key(id)
) engine=InnoDb;
CREATE TABLE MATHS_ANSWER(
id integer not null auto_increment,
date date default null,
question_no int not null,
question_id int not null,
solution varchar(128) default null,
solution_url varchar(128) default null,
primary key(id),
foreign key(question_id) references MATHEMATICS(id) ON DELETE NO ACTION ON UPDATE NO ACTION
) engine = InnoDb;
实体类:
@Entity
@Table(name = "Mathematics")
public class Mathematics {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id;
@Column(name = "year")
private Date year;
@Column(name = "question_no")
private Integer questionNo;
@Column(name = "question")
private String question;
@OneToOne(mappedBy = "maths", fetch = FetchType.EAGER,cascade = CascadeType.ALL
)
private MathsAnswers answers = new MathsAnswers();//getters & setters
MathsAnswers.java:
@Entity
@Table(name = "Mathematics")
public class Mathematics {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id;
@Column(name = "year")
private Date year;
@Column(name = "question_no")
private Integer questionNo;
@Column(name = "question")
private String question;
@OneToOne(mappedBy = "maths", fetch = FetchType.EAGER,cascade = CascadeType.ALL
)
private MathsAnswers answers = new MathsAnswers();//getters & setters
jpaRepo:
@RepositoryRestResource(collectionResourceRel = "mathematics", path = "maths")
public interface MathsRepo extends JpaRepository<Mathematics, Integer> {
}
post请求:
{
"year":"2004-01-03",
"questionNo":"4",
"question":"How many weeks makes a year?"
}
为MathsAnswer
实体指定的表名错误;应该是Mathsanswer
而不是Mathematics
。