title: JPA and Hibernate proxy behavior
body: <p>i tried to observe JPA2 / Hibernate4 proxy behavior below,</p>
  
  <p>// Circular entity with lazy loading:</p>
  
  <pre><code>@Entity
  public class Employee {
  
   @Id@Generated
   int id;
   String name;
   @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   Employee boss;
  
   public String toString() {
    return id + "|" + name + "|" + boss;
   }
  
   //getters and setters ...
  
  }
  </code></pre>
  
  <p>// Persist entities:</p>
  
  <pre><code>// Outer entity:
  Employee employee = new Employee();
  employee.setName("engineer");
  // Inner entity:
  Employee boss = new Employee();
  boss.setName("manager");
  employee.setBoss(boss);
  
  entityTransaction.begin();
  entityManager.persist(employee);
  entityTransaction.commit();
  System.out.println(employee);
  </code></pre>
  
  <p>// Output:</p>
  
  <pre><code>Hibernate: insert into Employee (id, boss_id, name) values (default, ?, ?)
  Hibernate: insert into Employee (id, boss_id, name) values (default, ?, ?)
  
  2|engineer|1|manager|null
  </code></pre>
  
  <p>// Load the outer entity:</p>
  
  <pre><code>String queryString = "select e from Employee e where e.id=" + employee.getId();
  Query query = entityManager.createQuery(queryString);
  Object loadedEmployee = query.getSingleResult();
  System.out.println(loadedEmployee.getClass().getSimpleName());
  </code></pre>
  
  <p>// Output:</p>
  
  <pre><code>Hibernate: select employee0_.id as id2_, employee0_.boss_id as boss3_2_, employee0_.name as name2_ from Employee employee0_ where employee0_.id=2 limit ?
  
  Employee
  </code></pre>
  
  <p>To my surprise the loaded outer entity above is still the plain one, but i'd expected it to be <code>Hibernate proxy</code> resulting from <code>lazy loading</code>. I could have missed something here, so how to get it right? A simple yet concrete example is much appreciated!</p>
  
  <p><strong>@EDIT</strong></p>
  
  <p>According to the answer from <code>@kostja</code> i adapted the code and debugged it in SE mode below, neither could <code>LazyInitializationException</code> be produced, nor was <code>boss property</code> proxied. Any further hints?</p>
  
  <p><img src="https://i.stack.imgur.com/2aUW4.png" alt="code window"></p>
  
  <p><img src="https://i.stack.imgur.com/IegSY.png" alt="debug window"></p>
  
  <p><strong>@EDIT 2</strong></p>
  
  <p>Finally, i'd confirm that the answer from <code>@kostja</code> is undoubtly great. </p>
  
  <p>I tested in EE mode, so the <code>proxied boss property</code> was observed below,</p>
  
  <p>// <code>LazyInitializationException</code> thrown:</p>
  
  <pre><code>public Employee retrieve(int id) {
   Employee employee = entityManager.find(Employee.class, id);
   // access to the proxied boss property outside of persistence/transaction ctx
   Employee boss = employee.getBoss();
   System.out.println(boss instanceof HibernateProxy);
   System.out.println(boss.getClass().getSimpleName());
   return boss;
  }
  </code></pre>
  
  <p>// Green light after put <code>Spring Tx</code> in place: </p>
  
  <pre><code>@Transactional
  public Employee retrieve(int id) ...
  </code></pre>
  
  <p>// Output:</p>
  
  <pre><code>true
  Employee_$$_javassist_0
  </code></pre>
  
  <p>Also, one can refer to <a href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch20.html#performance-fetching-initialization" rel="nofollow noreferrer">20.1.4. Initializing collections and proxies</a> from Hibernate docs.</p>