Consider the following code with an OpenSessionInView interceptor that
a) opens a session and starts a transaction before this code is run
b) commits the transaction and closes the session after this code is run
Code:
public void someMethod {
user = getUserLoggedIn();
if (user!=null){
selectedRoles = fillSelectedRolesFromUser(user);
}
roleList = this.getRolesDAO().getAll();
edit = true;
return "edit";
}
public UserItf getUserLoggedIn(){
Subject subject = (Subject) session.get(StringUtilities.SUBJECTKEY);
UserPrincipal principal = (UserPrincipal) subject.getPrincipals(UserPrincipal.class).iterator().next();
UserItf user = userDAO.findByUserName(principal.getUsername());
return user;
}
The field user in someMethod is a field property that my MVC automatically injects into this class.
Now consider:
a) forget that the opensessioninview shouldn't be responsible for the transaction.
b) when the code reaches someMethod, user.getFullName() = "someString".
c) the database entry has fullname = "other string"
Now:
1) I am not saving/updating the saved entity
2) I am setting user = theSavedInstance (the getUserLoggedIn method)
3) Debugging the code, getUserLoggedIn is really fetching the savedInstance (with the actual id, and "other string" as fullname
Why is my session updating the fullname to "someString" on commit? Even if automatic dirty checking is enabled, when I do user = savedInstance I was expecting that hibernate would forget about "someString", but it seems to be merging it onto the savedInstance.
Do you see anything I am missing that could cause this behaviour?
Thank you