I try to update the record but I get errors, in an attempt to prevent them from occurring, I decided that I was doing something wrong. Tell me, what am I doing wrong?
there is a class User:

@Entity @Table(name = "user") public class User { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name="name") private String name; @Column(name="name") private String login; //getters and setters } 

UserDao:

 @Repository public class UserDao{ @Autowired private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void updateUser(User user) { Session session = this.sessionFactory.getCurrentSession(); session.update(user); } public User getForId(Integer id) { Session session = this.sessionFactory.getCurrentSession(); User user = (User) session.load(User.class, new Integer(id)); return user; } } 

Controller:

 @RequestMapping(value = "/update", method = RequestMethod.POST) public void update(User user) { service.updateUser(user); } @RequestMapping(value = "/updateUser/{id}") public String updateUser(@PathVariable("id")int id, Model model) { User user = service.getForId(id); model.addAttribute("user",user); return "update"; } 

update.jsp:

 <form:form action="/update" method="post" commandName="user"> <table> <tr> <td><form:label path="name">name </form:label></td> <td><form:input path="name" /></td> </tr> <tr> <td><form:label path="login">name </form:label></td> <td><form:input path="login" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="Ok" onclick="/" /></td> </tr> </table> </form:form> 

this is a button on the created object that calls / updateUser

  <table> <c:forEach items="${users}" var="user"> <tr> <td><a href="delete/${user.id}">Удалить</a></td> <td><a href="updateUser/${user.id}">Изменить</a> </tr> </c:forEach> </table> 

mistake:

 org.hibernate.TransientObjectException: The given object has a null identifier: com.model.User at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.getUpdateId(DefaultSaveOrUpdateEventListener.java:270) at org.hibernate.event.internal.DefaultUpdateEventListener.getUpdateId(DefaultUpdateEventListener.java:70) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:238) at org.hibernate.event.internal.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:55) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) at org.hibernate.internal.SessionImpl.fireUpdate(SessionImpl.java:739) at org.hibernate.internal.SessionImpl.update(SessionImpl.java:731) at org.hibernate.internal.SessionImpl.update(SessionImpl.java:726) at com.dao.UserDAO.updateUser(UserDao.java:47) at com.service.UserService.updateUser(UserService.java:30) at com.service.UserService$$FastClassBySpringCGLIB$$12de773.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) at com.service.UserService$$EnhancerBySpringCGLIB$$e21bb612.updateUser(<generated>) at com.controller.UserController.update(UserController.java:80) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) 
  • Add the full text of the error to the question. - Sergey Gornostaev
  • @SergeyGornostaev Did - user255468
  • @SergeyGornostaev here again is the error - org.hibernate.persistentobjectexception, the detached entity has passed to persist spring - user255468

2 answers 2

First, there is no field for the identifier in the form, so it is not set for the user argument. In addition, the commandName attribute commandName long obsolete, now modelAttribute used modelAttribute . Change the form so:

 <form:form action="/update" method="post" modelAttribute="user"> <form:hidden path="id" /> ... </form:form> 

Second, annotate the method parameter:

 @RequestMapping(value = "/update", method = RequestMethod.POST) public String update(@ModelAttribute("user") User user) { service.updateUser(user); return "redirect:/some-success-url"; } 

Finally, in user you will get an instance of the User class, containing data, but not associated with the Hibernate session, in the detached state . The easiest way to solve this problem is to replace the call to session.update(user) with session.saveOrUpdate(user) in the UserDAO.updateUser() method.

  • I made a check for null, corrected it according to your recommendations, but the error is still the same. - user255468
  • ))))) Yes, it is exactly that. Thank you very much! - user255468
  • This command now adds another object to the database, but does not change it. - user255468
  • This object exists, I just did not insert methods to add a user here - user255468
  • Thank you very much for your help, I have solved the problem. - user255468

I solved the problem, in the dao method I changed the method and everything began to work well.

 public void updateUser(User user) { Session session = this.sessionFactory.getCurrentSession(); User user1 = (User)session.load(User.class, user.getId()); user1.setName(user.getName()); user1.setLogin(user.setLogin()); session.update(user1); } 
  • You now have an additional request. - Sergey Gornostaev