【发布时间】:2020-07-17 09:52:31
【问题描述】:
我有两个具有以下元素的类:
@Document(collection = "ClassP")
Class P{
@Id
private String p_id;
}
@Document(collection = "ClassR")
Class R{
@Id
private String r_id;
private String item;
@DBRef
private P p;
@DBRef
private User user;
}
这是我的 PRepository:
public interface PMongoRepository extends CrudRepository<P, String>{
P findPById(String p_id);
}
我要做的是从 R 类更新item。我从前端获取更改的项目,在我的控制器中我有新项目。所以,从前端看是没有问题的。
在我的控制器中,我有以下代码:
@RequestMapping(value = "/editData", method = RequestMethod.POST, consumes = "application/json")
public ModelAndView editData(@RequestBody Map<String, String> new_items) {
ModelAndView modelAndView = new ModelAndView();
R copyOfExistingR= new R();
P pFound = pRepository.findPById(new_items.get("p_id"));
copyOfExistingR.setP(pFound);
copyOfExistingR.setItem(new_items.get("sep"));
copyOfExistingR.setUser(user);
rRepository.save(copyOfExistingR);
return modelAndView ;
}
但是代码没有按预期工作。在rRepository.save(copyOfExistingR); 上出现以下错误:
Cannot create a reference to an object with a NULL id.
pFound 不为空,我可以将其打印出来,但我不知道我在更新 R 类时做错了什么。如果有人可以帮助我,我将不胜感激。
【问题讨论】:
标签: java spring-boot model-view-controller spring-data