我有一个 Spring Data rest API,我试图在名为 comment 的实体中保存一条新记录。 该实体与另一个名为 department 的实体存在多对一关系。
当我调用此实体的 post api 时,出现空指针错误,但我将部门外键作为邮递员请求的一部分传递。我不确定为什么服务器端没有收到它。有人可以对它有所了解吗?
错误
"cause": {
"cause": {
"cause": null,
"message": "ERROR: null value in column \"department_id\" of relation \"comments\" violates not-null constraint\n Detail: Failing row contains (32, null, null, null, null, null)."
},
"message": "could not execute statement"
}
评论实体(删除了用于保存行的getter和setter
@Table(name="comments")
public class Comment implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="SEQUENCE_FOR_COMMENTS", sequenceName="SEQUENCE_FOR_COMMENTS",allocationSize = 1 )
@GeneratedValue(strategy=GenerationType.AUTO, generator="SEQUENCE_FOR_COMMENTS")
@Column(name="comment_id")
private Integer commentId;
private String comment;
@ManyToOne
@JoinColumn(name="department_id")
private Department department;
}
部门实体(删除了用于保存行的getter和setter
public class Department implements Serializable { private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="SEQUENCE_FOR_Department" )
@Column(name="department_id")
private Long departmentId;
}
邮递员请求
{
"departmentv1":{
"departmentId":163
},
"comment":"Apr292023"
}
好吧,我认为您首先没有传递 referencedColumnName 它的作用是它表示当前表中关联表(部门)和当前表(评论)之间的关系当前实体是 commentId,它基本上是一个外键和一个关联表(部门)你有你的主键丢失了,但我认为如果你忘记提及它,如果他们都有相同的名字,它不会影响这么大
//实体层
//例如这里是一个示例关联表(类别)
@Table(name = "categories")
public class Categories
{
@Id
@Column(name = "cat_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Column(name = "cat_name")
private String catName;
}
//这是当前表(子类别)
@Table(name = "subcategories")
public class SubCategories
{
@Id
@Column(name = "sub_cat_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Column(name = "sub_cat_name")
private String subCatName;
//it denotes the relationship between current entity(name = "cat_id") in
present table(Sub_category) with the referencedColumnName = "cat_id"
present in associated table(Categories)
@JoinColumn(name = "cat_id", referencedColumnName = "cat_id")
@ManyToOne(fetch = FetchType.LAZY)
private Categories categories;
}
//存储层
//类别
public interface CategoriesRepository extends JpaRepository<Categories,
Long>
{
}
//子类别
public interface SubCategoriesRepository extends
JpaRepository<SubCategories, Long>
{
}
//服务层逻辑
public ResponseEntity<Map<String, Object>>
addSubCategory(AddSubCategoryRequest addSubCategoryRequest)
{
SubCategories newSubCategoryCategories = new SubCategories();
newSubCategoryCategories.setSubCatName(addSubCategoryRequest.
getSubCatName());
newSubCategoryCategories.setCategories(addSubCategoryRequest.
getCategories());
Map<String, Object> map = new LinkedHashMap<>();
map.put("data",
subCategoriesRepository.save(newSubCategoryCategories));
map.put(sts, true);
map.put(msg, "Sub-Category added success");
return ResponseEntity.ok().body(map);
}
//控制器层
@PostMapping("/admin/add/sub/category")
public ResponseEntity<Map<String, Object>> addSubCategory(@RequestBody
@Valid AddSubCategoryRequest subCategory)
{
return service.addSubCategory(subCategory);
}
//邮差请求
{
"subCatName":"T-shirts",
"categories":{
"id":1,
"catName":"Clothing and fashion"
}
}
我在这里还可以看到一件事,您只是传递 departmentId 而不是 department 表的其他值希望这个例子能解决您的问题,如果没有,请告诉我
看起来问题出在您在 Postman 请求中发送的 JSON 负载上。您在有效负载中有一个名为“departmentv1”的键,但您的 Comment 实体需要一个名为“department”的键。
更新您的 Postman 请求以匹配预期格式:
{
"department": {
"departmentId": 163
},
"comment": "Apr292023"
}
这应该可以解决问题,服务器端应该可以正确接收部门外键。