【发布时间】:2019-12-25 13:52:52
【问题描述】:
我正在尝试对我的 Spring Boot 后端进行后期调用,以便我可以存储一些物品。我要存储的对象中有其他对象,这是数据库的当前设计。
现在我想做一个 post call 来在数据库中创建一个 partused 对象。我将此 Json 对象发送到 Spring Boot,但收到 amount_type_id 不能为空的错误。所以我认为它无法解析对象,任何人都可以看到我的类/json对象有什么问题,因为我找不到问题
{
"amount":"2",
"specification":"test",
"partType":{
"idPartType":4,
"type":"leveringen"},
"amountType":{
"idAmountType":5,
"type":"m3"},
"project":{
"idProject":4,
"description":"test",
"isFinished":false,
"startDate":"2019-12-11",
"finishDate":null,
"name":"test2"
}
}
我的零件使用类
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonSerialize
private Long idPartsUsed;
private int amount;
private String specification;
@OneToOne
@JoinColumn(name = "part_type_id", referencedColumnName = "idPartType")
private PartType partType;
@OneToOne
@JoinColumn(name = "amount_type_id", referencedColumnName = "idAmountType")
private AmountType amountType;
@OneToOne
@JoinColumn(name = "project_id", referencedColumnName = "idProject")
private Project project;
我的金额类型类
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonSerialize
private Long idAmountType;
private String type;
错误信息
{
"cause": {
"cause": {
"cause": null,
"message": "Column 'amount_type_id' cannot be null"
},
"message": "could not execute statement"
},
"message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
}
【问题讨论】:
-
请发布更多信息,例如您遇到的错误、API 代码等
-
@SurajGautam 我已经为你添加了错误信息,我刚刚得到一个基本的 400 代码
-
您好,为什么要自动递增 AmountType 类中 idAmountType 的主键并以 json 格式发送?
-
@SurajGautam 我认为我需要为 post 调用指定 id 否则它会在数据库中创建一个我不想要的新 AmountType,我希望它链接到现有的 AmountType
-
在您的请求中,“amount”:“2”,但是在partsused类中数量数据类型是int,您可以在请求中尝试使用“amount”:2吗?
标签: java json spring spring-boot