Spring boot 如何从数据库更新对象而不插入新对象?

Spring boot 如何从数据库更新对象而不插入新对象?,spring-boot,hibernate,jpa,thymeleaf,Spring Boot,Hibernate,Jpa,Thymeleaf,我想从数据库中更新对象,而不向数据库中插入新的对象 以下是两种方法: @RequestMapping("/edit/{id}") public ModelAndView showEditProductPage(@PathVariable(name = "id") Integer id, Model model) { ModelAndView modelAndView = new ModelAndView("edit_cu

我想从数据库中更新对象,而不向数据库中插入新的对象

以下是两种方法:

@RequestMapping("/edit/{id}")
    public ModelAndView showEditProductPage(@PathVariable(name = "id") Integer id, Model model) {

        ModelAndView modelAndView = new ModelAndView("edit_customer");

        Customer customer = customerService.getCustomer(id);
        //Customer customer = customerRepository.getOne(id);
        //customer.setCustomerId(customerRepository.getOne(id).getCustomerId());
        System.out.println(customer.getCustomerId());
        System.out.println(customer.toString());
        model.addAttribute("customer", customer);
        modelAndView.addObject("customer", customer);
        return modelAndView;
    }
输出为:id=1客户{id=1,firstName='Krzysztof',address='ulica 1',phoneNumber='123456789'}

@RequestMapping("/update/{id}")
    public String updateCustomer(@PathVariable(name = "id") Integer id,
                                 @ModelAttribute("customer") Customer customer){
        //System.out.println(id);
        //customer.setCustomerId(id);
        System.out.println("____________________");
        System.out.println("customer id " + customer.getCustomerId());
        System.out.println("____________________");
        customer.setCustomerId(id);
        System.out.println("customer id " + customer.getCustomerId());
        System.out.println("owner " + customer.getOwner());
        System.out.println("address " + customer.getAddress());
        System.out.println("phone number " + customer.getAddress());
        System.out.println("to string: " + customer.toString());
        System.out.println("id " + id);
        customerService.save(customer);

        return "redirect:/";
    }
第二个方法之后的输出是:id=1客户{id=null,firstName='Krzysztof',address='ulica 1zzz',phoneNumber='123456789'}

@RequestMapping("/update/{id}")
    public String updateCustomer(@PathVariable(name = "id") Integer id,
                                 @ModelAttribute("customer") Customer customer){
        //System.out.println(id);
        //customer.setCustomerId(id);
        System.out.println("____________________");
        System.out.println("customer id " + customer.getCustomerId());
        System.out.println("____________________");
        customer.setCustomerId(id);
        System.out.println("customer id " + customer.getCustomerId());
        System.out.println("owner " + customer.getOwner());
        System.out.println("address " + customer.getAddress());
        System.out.println("phone number " + customer.getAddress());
        System.out.println("to string: " + customer.toString());
        System.out.println("id " + id);
        customerService.save(customer);

        return "redirect:/";
    }
客户存储库扩展了JPA存储库, 保存方法:

public void save(Customer customer) {
        customerRepository.save(customer);
    }
thymeleaf模板:

!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8" />
    <title>Edit Customer</title>
    <link rel="stylesheet" type="text/css" th:href="@{/table.css}" />
</head>
<body>
<div align="center">
    <h1>Edit Customer</h1>
    <a href="/">Go Back</a>
    <br />
    <form action="#" th:action="@{'/update/' + ${id}}" th:object="${customer}"
          method="post">
        <table border="0" cellpadding="10">
            <tr>
                <td>Customer ID:</td>
                <td>
                    <input type="text" th:field="*{customerId}" readonly="readonly" />
                </td>
            </tr>
            <tr>
                <td>First Name:</td>
                <td>
                    <input type="text" th:field="*{owner}" />
                </td>
            </tr>
            <tr>
                <td>Address:</td>
                <td><input type="text" th:field="*{address}" /></td>
            </tr>
            <tr>
                <td>Phone Number:</td>
                <td><input type="text" th:field="*{phoneNumber}" /></td>
            </tr>
            <tr>
                <td colspan="2"><button type="submit">Save</button> </td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>
尽管我尝试将id设置为1或任何其他数字,但它仍然为空。
有人能告诉我为什么以及如何解决它吗?

问题在您的客户类设置器中:

public void setCustomerId(Integer id){

    this.customerId = customerId;
}

您将字段分配给了它自己,这就是为什么它始终为空。应该是“this.customerId=id”:。

问题在客户类的setter中:

public void setCustomerId(Integer id){

    this.customerId = customerId;
}

您将字段分配给了它自己,这就是为什么它始终为空。应该是“this.customerId=id”:。

发布您的
客户
实体类我怀疑您的
id
生成策略有问题,我猜您的id不是
自动增量
?发布在原始帖子底部的是自动增量,我将立即使用sql编辑帖子设置此:
@GeneratedValue(strategy=GenerationType.AUTO)
发布您的
客户
实体分类我怀疑您的
id
生成策略有问题,我猜您的id不是
autoincrement
?发布在原始帖子底部它是autoincrement,我将立即用sql编辑帖子设置此:
@GeneratedValue(策略=GenerationType.AUTO)