title: Persist a value of an object when it is set
body: <p>I am a completely new to JPA+Hibernate+Spring (but I have a fairly much experience with Spring framework for web application and SQL) so my question here may not be using the correct technical term in JPA.</p>
  
  <p>I have a typical <code>User</code> POJO which is annotated with <code>javax.persistence.*</code> and I have set up hibernate+hsqldb according to examples I found which are currently working fine. The <code>User</code> class looks like this :</p>
  
  <pre><code>@Entity
  @Table(name = "USER")
  public class User {
  
      private Long id;
      private String name;
      // Ignore id getter and setter methods
  
      @Column(name = "USER_NAME")
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  }
  </code></pre>
  
  <p>This is my <code>UserDAOImpl</code> class.</p>
  
  <pre><code>public class UserDAOImpl implements UserDAO {
  
      private HibernateTemplate hibernateTemplate;
  
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.hibernateTemplate = new HibernateTemplate(sessionFactory);
      }
  
      @Override
      public void saveUser(User user) {
          hibernateTemplate.saveOrUpdate(user);
      }
  
      // This method is made to return only one User object.
      @Override
      @SuppressWarnings("unchecked")
      public User getGiUser() {
          return hibernateTemplate.find("from User where User_Name ='gi'").get(0);
      }
  }
  </code></pre>
  
  <p>By using <code>getGiUser</code> method, I can get the persistent object from the database (If I understand correctly). My question is whether it is possible to make the <code>User.setName()</code> method to commit immediately to database when it is called without having to save it manually? How or where should I start or learn about this?</p>