title: JSON serializing and deserializing with hibernate jpa to have parent object into child in JSON response
body: <p>I am developing rest web app with spring framework, Hibernate and JSON. Please Assume that I have two entities like below:</p>
  
  <p>BaseEntity.java</p>
  
  <pre><code>@MappedSuperclass
  @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id" )
  public abstract class BaseEntity implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private long id;
      public long getId() {
          return id;
      }
  }
  </code></pre>
  
  <p>University.java</p>
  
  <pre><code> public class University extends BaseEntity {
  
        private String uniName;
  
         @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER,orphanRemoval = true)
        @JoinColumn(name = "university_id")
          private List&lt;Student&gt; students=new ArrayList&lt;&gt;();
      // setter an getter
      }
  </code></pre>
  
  <p>Student.java</p>
  
  <pre><code>    public class Student extends BaseEntity{
  
          private String stuName;
  
          @ManyToOne(fetch = FetchType.EAGER)
          @JoinColumn(name = "university_id",updatable = false,insertable = false)   
          private University university;
  
      // setter an getter
          }
  </code></pre>
  
  <p>when I call my rest api to list University every things work fine as I expect, but when I call my rest api to list student eagerly my JSON response is</p>
  
  <pre><code>[
     {
      "id": 1,
      "stuName": "st1",
      "university": {
          "id": 1,
          "uniName": "uni1"
                   }
      },
      {
          "id": 2,
          "stuName": "st2",
          "university": 1
      }
  ]
  </code></pre>
  
  <p>but my <strong>desired</strong> response is:</p>
  
  <pre><code>[
      {
          "id": 1,
          "stutName": "st1",
          "university": 
          {
           "id": 1,
          "uniName": "uni1"
          }
      },
      {
          "id": 2,
          "stutName": "st2",
          "university": 
          {
           "id": 1,
          "uniName": "uni1"
          }
      }
  </code></pre>
  
  <p><strong>Update 1</strong>: my hibernate annotation working fine I have JSON issue</p>
  
  <p>Requirements :</p>
  
  <ol>
  <li><p>I need both side fetch eagerly(the university side is Ok)</p></li>
  <li><p>I need university object in student side for every student(when I fetching student eagerly)</p></li>
  </ol>
  
  <p>What kind of serialization or JSON config I need to do that for matching my desired response?</p>
  
  <p><strong>Update 2:</strong> </p>
  
  <p>by removing <em>@JsonIdentityInfo</em> and editing student side like below:</p>
  
  <blockquote>
  <pre><code>@ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "university_id",updatable = false,insertable = false)
  @JsonIgnoreProperties(value = "students", allowSetters = true)
  private University university;
  </code></pre>
  </blockquote>
  
  <p>the json response still same 
  I need my <strong>desired</strong> response that is mentioned above.</p>
  
  <p>thanks</p>