title: Hibernate persistent object behavior
body: <p>I was trying to learn about different states of objects in Hibernate. I tried the following and could not find an explanation to the behavior shown. Can anyone help?</p>
  
  <p>Here is what I am trying to do: Insert a new record in Employee table (empId being primary key). In the same transaction, update the newly added record (using a query, modifying empName). Then when I am checking empName property of the persistent object it continues to show the old empName value. Being a persistent object, I expected it to reflect the change made in the database. I could not understand why it didn't. (My hibernate config file has everything set to default except for "hibernate.hbm2ddl.auto" property set as update)
  However, after doing update, when I set the empName of the persistent object with value returned by getEmpName (which displays as the old empName value by sysout), the final data in the table shows the new empName value (i.e the one I updated using hql). Please refer to the code for this:</p>
  
  <pre><code>Transaction tx = session.getTransaction();
      tx.begin();
  
      Employee e1 = new Employee();
      e1.setEmpId(1);
      e1.setEmpName("Jack");
      e1.setEmpAge(25);
      session.save(e1);
      System.out.println("before: "+e1.getEmpName()); //prints Jack
      session.createQuery("update Employee set empName = \'Jack_new\' where id=1").executeUpdate();
      System.out.println("after: "+e1.getEmpName()); //prints Jack
      e1.setEmpName(e1.getEmpName()); //should update database
      tx.commit(); //sets empName value to Jack_new, as seen in table
      System.out.println("last: "+e1.getEmpName()); //prints Jack
  </code></pre>