title: can I access hibernate persisted objects through property members (instead of accessors )?
body: <p>I understand that the idiom is to define your member variables as private and auto-create getters and setters, and use that in hibernate code.</p>
  
  <p>but I am just curious whether it would work if I don't define getters/setters, and directly refer to the member variables/properties (now made public).</p>
  
  <p>in the following code App.java, the line</p>
  
  <p>"
              System.out.println("loaded " + emp2.account.getAccountNumber());
  "</p>
  
  <p>here emp2 is a persisted obj (loaded from db), I can actually refer to the "account" member directly, but somehow when I refer to account.accountNumber directly, it gives me a null, so I had to use account.getAccountNumber();</p>
  
  <p>so why are these 2 cases giving different results? </p>
  
  <p>--- somehow I seemed to have gotten the "direct member access" to work across all cases. but now I can't find how.</p>
  
  <p>Thanks
  Yang</p>
  
  <p>File: App.java</p>
  
  <pre><code>    package yy.learnhibernate;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  
  /**
   * Hello world!
   *
   */
  public class App {
      public static void main(String[] args) {
  
          SessionFactory factory = new Configuration().configure().buildSessionFactory();
  
          AccountEntity acct;
          EmployeeEntity emp;
          acct = new AccountEntity();
          acct.accountNumber= "fakeNumber";
          emp = new EmployeeEntity();
          emp.firstName = "f";
          emp.account = acct;
  
          Session session1 = factory.openSession();
          session1.beginTransaction();
          session1.save(acct);
          session1.save(emp);
  
          emp = new EmployeeEntity();
          emp.firstName = "f2";
          emp.account = acct;
          session1.save(emp);
          //
          session1.getTransaction().commit();
          session1.close();
  
          session1 = factory.openSession();
          session1.beginTransaction();
  
          AccountEntity acct2 = (AccountEntity) session1.load(AccountEntity.class, 1);
          EmployeeEntity emp2 = (EmployeeEntity) session1.get(EmployeeEntity.class, 1);
          System.out.println("loaded " + emp2.account.getAccountNumber());
  
          for (EmployeeEntity e : acct2.getEmployee()) {
              System.out.println("seen employee" + e.firstName);
          }
  
          acct2.setAccountNumber("changed ");
          // session1.update(acct2);
          session1.getTransaction().commit();
  
      }
  }
  </code></pre>
  
  <p>File: AccountEntity.java</p>
  
  <pre><code>package yy.learnhibernate;
  
  import java.io.Serializable;
  import java.util.HashSet;
  import java.util.Set;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "Account")
  public class AccountEntity implements Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1770939417652939285L;
      @Id
      @Column(name = "ID", unique = true, nullable = false)
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      public Integer accountId;
  
      @Column
      public String accountNumber;
  
  
      public String getAccountNumber() {
          return accountNumber;
      }
  
      public void setAccountNumber(String accountNumber) {
          this.accountNumber = accountNumber;
      }
  
      @OneToMany(mappedBy = "account")
  //  @Transient
      // We will define the association here
      public Set&lt;EmployeeEntity&gt; employee = new HashSet&lt;EmployeeEntity&gt;();
  
  
  
      public Integer getAccountId() {
          return accountId;
      }
  
      public void setAccountId(Integer accountId) {
          this.accountId = accountId;
      }
  
  
  
      public Set&lt;EmployeeEntity&gt; getEmployee() {
          return employee;
      }
  
      public void setEmployee(Set&lt;EmployeeEntity&gt; employee) {
          this.employee = employee;
      }
  //
  //  // Getters and Setters are not shown for brevity
  }
  </code></pre>
  
  <p>EmployeeEntity.java</p>
  
  <pre><code>package yy.learnhibernate;
  
  import java.io.Serializable;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.ManyToOne;
  import javax.persistence.OneToOne;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "Employee")
  public class EmployeeEntity implements Serializable {
      private static final long serialVersionUID = -1798070786993154676L;
  
      @Id
      @Column(name = "ID", unique = true, nullable = false)
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      public Integer employeeId;
      @Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
      public String firstName;
  
  
      @ManyToOne
      // We will define the association here
      public AccountEntity account;
  
  
      public Integer getEmployeeId() {
          return employeeId;
      }
  
      public void setEmployeeId(Integer employeeId) {
          this.employeeId = employeeId;
      }
  
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
  
  
      public AccountEntity getAccount() {
          return account;
      }
  
      public void setAccount(AccountEntity account) {
          this.account = account;
      }
  
      // Getters and Setters are not shown for brevity
  }
  </code></pre>