title: How to get just an entity id instead of the entire entity in JSON response
body: <p>I saw several similar questions but non with an answer that worked for me.
  I am working on converting an existing project over to use Hibernate. I am using Mysql for the database. I have a class like this:</p>
  
  <pre><code>@Entity
  @Table(name = "user")
  public class User {
  
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     @Column(name = "user_id")
     private Long userId;
  
     @ManyToOne
     @JoinColumn(name = "group_id_user")
     private Group group;
  
     @Column(name = "name")
     private String name;
  
     ...
     // getters and setters....   
  }
  </code></pre>
  
  <p>and a class like this:</p>
  
  <pre><code>@Entity
  @Table(name = "group")
  public class Group {
  
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     @Column(name = "group_id")
     private Long groupId;
  
     @Column(name="group_name")
     private String groupName;
  
     ...
     // getters and setters....   
  }
  </code></pre>
  
  <p>When I fetch a <code>User</code> item from the database I convert it to JSON and end up with a result in this format:</p>
  
  <pre><code>{
      "user": {
          "userId": 1,
          "group": {
              "groupId": 3,
              "groupName": "Group Three",
  
          },
          "name": "John Doe",
       }
  }
  </code></pre>
  
  <p>In my case, the <code>Group</code> object is actually quite a bit bigger than in this example and also contains references to another table as well (i.e. <code>Group</code> has a <code>SomeOtherClass</code> object). I want to end up with something that looks like the following:</p>
  
  <pre><code>{
      "user": {
          "userId": 1,
          "groupId": 3,
          "name": "John Doe",
       }
  }
  </code></pre>
  
  <p><strong>Is there a way to just get the groupId in my JSON response instead of the entire <code>Group</code> object?</strong></p>
  
  <p>I have tried adding FetchType.Lazy to my User class</p>
  
  <pre><code>@ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "group_id_user")
  private Group group;
  </code></pre>
  
  <p>but when I try to convert the <code>User</code> object to Json I get the following error:</p>
  
  <pre><code>error: Errorjava.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
  </code></pre>
  
  <p>I found <a href="https://stackoverflow.com/questions/13459718/could-not-serialize-object-cause-of-hibernateproxy">this answer</a> which gets rid of the error, but then the JSON object is exactly the same as it was before I added the <code>FetchType.LAZY</code>. It looks like somehow converting to JSON adds in the values that the <code>FetchType.LAZY</code> was ignoring. </p>
  
  <p>I am using converting to JSON as follows:</p>
  
  <pre><code>GsonBuilder b = new GsonBuilder();
  Gson gson = b.create();
  //b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);  
  
  com.google.gson.JsonArray jsonArray = gson.toJsonTree(out).getAsJsonArray();
  </code></pre>