如何在Spring Boot中使用save方法更新元素?

huangapple go评论79阅读模式
英文:

How to update an element in spring boot using save method?

问题

我有两个类,它们具有以下元素:

@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);处,我收到以下错误消息:

无法创建对具有NULL id的对象的引用。

pFound 不是null,我可以打印它,但是我不知道在更新R类时做错了什么。如果有人能帮助我,我将不胜感激。

英文:

I have two classes which have the following elements:

@Document(collection = &quot;ClassP&quot;)
Class P{
@Id
private String p_id;
}

 @Document(collection = &quot;ClassR&quot;)
Class R{
@Id
private String r_id;
private String item;

@DBRef
private P p;

@DBRef
private User user;
}

Here is my PRepository:

public interface PMongoRepository extends CrudRepository&lt;P, String&gt;{

P findPById(String p_id);
}

What I am going to do is to update the item from class R. I get the changed item from the frontend and in my controller I have the new item. So, there is no problem from the front end side.
In my controller, I have the following code:

@RequestMapping(value = &quot;/editData&quot;, method = RequestMethod.POST, consumes = &quot;application/json&quot;)
	public ModelAndView editData(@RequestBody Map&lt;String, String&gt; new_items) {

		ModelAndView modelAndView = new ModelAndView();

		R copyOfExistingR= new R();

		P pFound = pRepository.findPById(new_items.get(&quot;p_id&quot;));
			copyOfExistingR.setP(pFound);
            copyOfExistingR.setItem(new_items.get(&quot;sep&quot;));
            copyOfExistingR.setUser(user);
            rRepository.save(copyOfExistingR);

return modelAndView ;
}

But the code doesn't work as expected. On the rRepository.save(copyOfExistingR); I get the following error:

Cannot create a reference to an object with a NULL id.

The pFound is not null and I can print it out, but I don't know what I am doing wrong with updating the R class. I would be thankful if anybody could help me.

答案1

得分: 0

你从未设置过copyOfExistingR的id。

英文:

You never set id of copyOfExistingR.

答案2

得分: 0

解决方案是你只需要设置 'r_id'

@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();

    //-----在这里为对象 copyOfExistingR 设置你的 r_id----

    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;
}
英文:

The solution is that you just set the 'r_id'

@RequestMapping(value = &quot;/editData&quot;, method = RequestMethod.POST, consumes = &quot;application/json&quot;)
        public ModelAndView editData(@RequestBody Map&lt;String, String&gt; new_items) {
    
            ModelAndView modelAndView = new ModelAndView();
    
            R copyOfExistingR = new R();

          //-----Here you set your r_id for object copyOfExistingR----
    
            P pFound = pRepository.findPById(new_items.get(&quot;p_id&quot;));
                copyOfExistingR.setP(pFound);
                copyOfExistingR.setItem(new_items.get(&quot;sep&quot;));
                copyOfExistingR.setUser(user);
                rRepository.save(copyOfExistingR);
    
    return modelAndView ;
    }

答案3

得分: 0

问题是我在 P 类中 @Id 上方定义了一个变量,这是有问题的。@Id 必须始终是类中的第一项。

英文:

The problem was that I had defined a variable above @Id in the P class and it was problematic. The @Id must always be the first item in the class.

huangapple
  • 本文由 发表于 2020年4月5日 19:56:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61042185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定