通过Thymeleaf进行实体编辑时重新生成ID的问题

时间:2019-01-20 21:10:23

标签: hibernate spring-boot spring-data-jpa thymeleaf

我正在将SpringBoot 2.1.2与Hibernate和Thymeleaf一起使用。

当用户编辑条目(对象Person)时,将生成新ID。因此,创建新对象而不是更改对象的状态。其他字段保持不变。

Debug显示已编辑的模型从Thymeleaf页面移至Controller(save()方法),并生成了新ID。

library(shiny)
library(googleAuthR)

options(googleAuthR.webapp.client_id = "xxx")

ui <- fluidPage(

    titlePanel("Sample Google Sign-In"),

    sidebarLayout(
      sidebarPanel(
        googleSignInUI("demo")
      ),

      mainPanel(
        with(tags, dl(dt("Name"), dd(textOutput("g_name")),
                      dt("Email"), dd(textOutput("g_email")),
                      dt("Image"), dd(uiOutput("g_image")) ))
      )
    )
  )

server <- function(input, output, session) {

  sign_ins <- shiny::callModule(googleSignIn, "demo")

  output$g_name = renderText({ sign_ins()$name })
  output$g_email = renderText({ sign_ins()$email })
  output$g_image = renderUI({ img(src=sign_ins()$image) })

  observe({
    req(sign_ins()$name)

    print("This works")

  })

}

# Run the application 
shinyApp(ui = ui, server = server)

下面的百里香叶编辑页面的源代码:

    @Controller
public class PersonController {

    @Autowired
    private PersonService personService;

    @GetMapping("/person-list")
    public String list(final Model model) {
        final Collection<Person> persons = personService.findAll();
        model.addAttribute("persons", persons);
        return "person-list";
    }

    @GetMapping("/person-create")
    public String create() {
        personService.merge(new Person());
        return "redirect:/person-list";
    }

    @GetMapping("/person-remove")
    public String remove(@RequestParam("id") String id) {
        personService.removeById(id);
        return "redirect:/person-list";
    }

    @GetMapping("/person-edit")
    public String edit(@RequestParam("id") String id, Map<String, Object> model) {
        final Person person = personService.findOneById(id);
        model.put("person", person);
        return "person-edit";
    }

    @PostMapping("/person-save")
    public String save(@ModelAttribute("person") Person person) {
        personService.merge(person);
        return "redirect:/person-list";
    }

}

AbstractEntity如下:

    <body>
        <div th:replace="fragments/header.html"></div>

        <form th:action="@{/person-save}" th:object="${person}" method="post">
            <table width="100%" cellspacing="0" cellpadding="10" border="1">
                <tr>
                    <td width="200" nowrap="nowrap">ID</td>
                    <td>
                        <input type="text" th:field="*{id}"/>
                    </td>
                </tr>
                <tr>
                    <td>First Name</td>
                    <td>
                        <input type="text" th:field="*{firstName}"/>
                    </td>
                </tr>
                <tr>
                    <td>Middle Name</td>
                    <td>
                        <input type="text" th:field="*{middleName}"/>
                    </td>
                </tr>
                <tr>
                    <td>Last Name</td>
                    <td>
                        <input type="text" th:field="*{lastName}"/>
                    </td>
                </tr>
                <tr>
                    <td>E-mail</td>
                    <td>
                        <input type="text" th:field="*{email}"/>
                    </td>
                </tr>
            </table>

            <br/>
            <button type="submit">SAVE</button>
            <br/>
        </form>

        <div th:replace="fragments/footer.html"></div>
    </body>
</html>

以下显示的人员实体:

    @Getter
@MappedSuperclass
public abstract class AbstractEntity {

    @Id
    @NotNull
    @Column(name = "id")
    private String id = UUID.randomUUID().toString();

}

有什么想法吗?预先感谢。

0 个答案:

没有答案