Spring MVC Error @SessionAttributes!

I use 2.5 and do everything through annotations.

I have a simple form that allows the user to edit an object. The controller behind it creates an object and adds it to the GET model and processes the changes in POST (submit). It works great, but I donโ€™t understand why. The object is never explicitly added to the session, and the id object is not passed to the submit method. How does the controller send method know the "id" value of an object?

The reason for this question is because I have a different form / controller that is almost identical to the above, but does not work unless I add an object to @SessionAttributes. The difference is that the object this particular controller is working with has a link to another object that is lazily loaded (I use Hibernate behind the scenes). When I submit a form without putting the parent in SessionAttributes, I get a DataIntegrityViolationException because I never load the specified object.

When I add the parent to @SessionAttributes, the problem magically disappears. I speak magically, because although I put the parent in the session in GET, I still have never loaded an explicit reference object, so it should be empty (or an empty proxy server or something else).

What is happening in the world? I need help!

+3
source share
2 answers

The item is most likely recreated during the submit phase. The identifier can be โ€œsavedโ€ in a hidden form field.

You must be careful with the attributes of the session, I always try to avoid this if I have no good reason. In combination with Hibernate, it can create all kinds of headaches.

If you need to, use a fully initialized copy.

+2
source

How does the controller send method know the "id" value of an object?

"id". , Spring . Hibernate , .

+1

Source: https://habr.com/ru/post/1707778/


All Articles