Recently I started to get acquainted with Spring, MySQL, Hibernate, etc. As a practice I try to make a CRUD application. Display a table on the page, add / delete a record, everything works. But with editing problems.

The entity class has only 2 fields: id and name . Here are the controller's methods (the first to get the edit page of a specific record, the second to change this record):

 @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET) public String edition(@PathVariable("id") int id, Model model){ model.addAttribute("entity", service.get(id)); return "edition"; } @RequestMapping(value = "/edit", method = RequestMethod.POST) public String edit(@ModelAttribute("entity") TestEntity testEntity){ service.edit(testEntity); return "redirect:/"; } 

Repository Method:

 public void edit(TestEntity testEntity) { sessionFactory.getCurrentSession().update(testEntity); } 

And the body of the edit page:

 <h2>${entity}</h2> <c:url value="/edit" var="edit"/> <form:form action="${edit}" method="POST" modelAttribute="entity"> <label for="name">NAME</label> <input type="text" name="name" id="name"> <input type="submit" value="Edit"> </form:form> 

Those. I pass the attribute attribute to the edit page (I specifically added the output on the ${entity} page to see exactly what the required record is from the database), then I try to change it. But when I hit submit and the edit method is called, for some reason it tries to change the entry with id = 0 (which is not present) and an exception is thrown:

 Message Request processing failed; nested exception is org.springframework.orm.hibernate5.HibernateOptimisticLockingFailureException: Object of class [test1.entity.TestEntity] with identifier [0]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [test1.entity.TestEntity#0] Description The server encountered an unexpected condition that prevented it from fulfilling the request. Exception org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.hibernate5.HibernateOptimisticLockingFailureException: Object of class [test1.entity.TestEntity] with identifier [0]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [test1.entity.TestEntity#0] org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:901) javax.servlet.http.HttpServlet.service(HttpServlet.java:660) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 

I really will not take what I am doing wrong. Versions I use Spring 5.1.1, Hibernate 5.3.7

  • In my opinion, editing is RequestMethod.PUT) - Oleksiy Morenets
  • I tried now to make the PUT method in the controller (well, I removed the POST on the form page), writes: "Request method 'POST' not supported". Immediately it does not matter what method we have there, the "submit" button and so sends data to the server, makes a POST request. - SoulDrake 5:06

1 answer 1

This is because you do not pass the id parameter to the controller. In order for the server to know which record from the database needs to be updated, it is required to transfer the id value.

This is usually done by adding an invisible field to the form containing the desired value.

 <input type="hidden" name="id" value="${id}"> 
  • Yes, exactly, it helped)) Thank you very much - SoulDrake