【发布时间】:2017-08-18 03:21:35
【问题描述】:
我有两个具有@ManyToOne 关系的实体类,如下所示。
@Entity
public class Student {
@Id
private Integer studentId;
@Column(nullable = false)
private String studentName;
@ManyToOne(targetEntity = School.class, fetch = FetchType.LAZY, optional = false)
@JoinColumn(referencedColumnName = "schoolId", insertable = false, updatable = false)
private School school;
//Getters and Setters methods
.
@Entity
public class School {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer schoolId;
@Column(nullable = false)
private String schoolName;
//Getters and Setters methods
当我尝试使用带有 JSON 有效负载的 CrudRepository 默认方法 studentRepository.save(student) 保存学生对象时,它给了我一个错误 java.sql.SQLException: Field 'school_id' doesn't have a default value。当我在调试模式下运行时,我可以看到 School 对象设置正确。
我的 JSON 负载如下:
[
{
"studentId": "2015020",
"studentName": "ABC",
"school": {
"schoolId": 1
}
}
]
我是 Spring Data JPA 的新手,所以这可能是一个非常基本的错误。
【问题讨论】:
标签: java json hibernate jpa spring-data