我有兩個實體類,它們具有@ManyToOne關係,如下所示。用外鍵使用CrudRepository保存實體對象
@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
當我嘗試使用CrudRepository默認方法studentRepository.save(student)
用JSON的有效載荷,以節省學生對象,它給了我一個錯誤java.sql.SQLException: Field 'school_id' doesn't have a default value
。當我在調試模式下運行時,我可以看到School對象設置正確。
我的JSON有效載荷如下:
[
{
"studentId": "2015020",
"studentName": "ABC",
"school": {
"schoolId": 1
}
}
]
我是新來的春天JPA的數據,所以這可能是一個非常基本的錯誤。
感謝您指出。這是一個錯字,我糾正了它。 – anirus