title: JPA persistence and Hibernate javassist objects
body: <p>I am using Play Framework v.1.2.6 for a project and I have issues with hibernation of objects when i am trying to retrieve them from database.
  <a href="https://www.playframework.com/documentation/1.2.x/jpa" rel="nofollow noreferrer">https://www.playframework.com/documentation/1.2.x/jpa</a></p>
  
  <p>I have a query which selects rows from a mysql table and in the result there are proxy objects. Please see image below.<a href="https://i.stack.imgur.com/Lsgvw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lsgvw.png" alt="enter image description here"></a></p>
  
  <p>I am not using any Hibernate configuration file. I am using only @Annotations in my model class.</p>
  
  <p>Example of my query:</p>
  
  <pre><code>static List&lt;User&gt; getAllUsersFromAccount(Account account) {
      return  User.find(
              "SELECT u " +
              "FROM User u " +
              "WHERE u.account=?", 
              account).fetch();
  }
  </code></pre>
  
  <p>Does anyone knows how can i avoid getting proxy objects in the query results?</p>
  
  <p>For now we iterate over the array of users and check if the entity is instanceof HibernateProxy. We do not want to do that because maybe we have the same problem and for other queries.</p>
  
  <pre><code>private static &lt;T&gt; T initializeAndUnproxy(T entity) {
      Hibernate.initialize(entity);
      if(entity instanceof HibernateProxy) {
          entity = (T)((HibernateProxy)entity).getHibernateLazyInitializer().getImplementation();
      }
      return entity;
  }
  </code></pre>