title: How to properly convert List of specific objects to Gson?
body: <p>I am working on Spring MVC project. I am using Hibernate. I want to use AJAX with jQuery to get some JSONs from my Spring Controllers. Unfortunately when I was implementing <code>Gson</code> methods in my application I have got an error: </p>
  
  <pre><code>java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: 
  org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
  </code></pre>
  
  <p>Which adapter I have to use and in which way? The error has occurred on the last line of the method:</p>
  
  <pre><code>public String messagesToJson(List&lt;Message&gt; messages) {
      Gson gson = new Gson();     
      List&lt;Message&gt; synchronizedMessages = Collections.synchronizedList(messages);
      return gson.toJson(synchronizedMessages, ArrayList.class);
  }
  </code></pre>
  
  <p>This is my <code>Message</code> class I am using in my Spring MVC project with Hibernate:</p>
  
  <pre><code>@Entity
  @Table(name = "MESSAGES", schema = "PUBLIC", catalog = "PUBLIC")
  public class Message implements java.io.Serializable {
  
      private static final long serialVersionUID = 1L;
      private int messageId;
      private User users;
      private String message;
      private Date date;
  
      //Constructor, getters, setters, toString
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I am wondering: my <code>Message</code> object is proxied or the whole <code>List&lt;Message&gt;</code>? I am getting the list of messages in this way:</p>
  
  <pre><code>public List&lt;Message&gt; findAllUserMessages(String username) {
      Query query = entityManager.createQuery("from Message where username = :username order by date desc")
              .setParameter("username", username);
  
      @SuppressWarnings("unchecked")
      List&lt;Message&gt; messages = query.getResultList();
      return messages;
  }
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>No, my <code>List&lt;Message&gt;</code> object isn't proxied.</p>