Im having a hard time figuring out why something is happening in my application.
Using spring and Hibernate and struts2 there is some really strange stuff i noticed during debugging.
I have a user-form, where a user can edit his information. When this form is submitted, the controller’s save() method should do some stuff and save the user.
The strange thing is, that before I do anything in the controller, i want to find information about who is doing this change. So i fetch a User-object from the db based on the Id of whoever is submitting the form. But when this user-object is fetched. Then automaticly the user in the form is submitted…
Im fetching the user like this:
User createdByUser = (User) getHibernateTemplate().find("from User u where u.username=?", username).get(0);
So apparantly some mechanism is auto-committing my user.
Most of the time, my createdByUser is the same user as the submitted user. Could there be that hibernate detects this and somehow tries to synchronize the db?
Probably it’s because you are making changes on managed entity. So the changes gets synchronized with the database & the next time you fetch that entity, you are getting the entity with changes reflected, implicitly.
Either after performing search, you can clear the persistence context to detach all the underlying entities or can detach the single entity explicitly from the session.
From documentation :