-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Why is this automatically dirty checking my object?
PostPosted: Thu May 26, 2011 7:26 am 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.