title: Minimize JSON to deserialize and persist complex POJO with JPA/Hibernate
body: <p>I have a fairly complex POJO that I need to deserialize from a JSON string and persist in a MySQL database. The following is a very simplified example class:</p>
  
  <pre><code>@Entity
  @Table(name="a")
  public class A{
      private Long id;
      private B b;
      private C c;
      private D d;
  }
  
  @ManyToOne
  @JoinColumn(name="b_id")
  public B getB(){
      return this.b;
  }
  
  public void setB(B b){ this.b = b; }
  
  @ManyToOne
  @JoinColumn(name="c_id")
  public C getC(){ 
      return this.c;
  }
  
  public void setC(C c){ this.c = c; }
  
  @ManyToOne
  @JoinColumn(name="d_id")
  public D getD(){ 
      return this.d;
  }
  
  public void setD(D d){ this.d = d; }
  </code></pre>
  
  <p>Each class <code>B</code>, <code>C</code>, and <code>D</code> also have a number of fields and objects (some with even more required objects and fields) that can not be null according to the database schema, which I can't change. I can deserialize and persist this no problem, but the JSON required to do so is really massive.  I only need to persist the deserialized <code>A</code>, so I really just need the <code>_id</code> fields from <code>B</code>, <code>C</code>, and <code>D</code>. </p>
  
  <p>Right now my JSON is something like:</p>
  
  <pre><code>{
    "id":1, 
    "b":{"id":2, ...}, 
    "c":{"id":3, ...}, 
    "d":{"id":4, ...}
  }
  </code></pre>
  
  <p>where I have to fill in all the non-nullable database fields. What I would like to do is read a JSON string like:</p>
  
  <pre><code>{
    "id":1,
    "b_id":2,
    "c_id":3,
    "d_id":4
  }
  </code></pre>
  
  <p>and just have Hibernate/JPA update those fields in the database. I think the real tricky part is that other classes/methods in my application will need the entire object hierarchy for reading from the database. The only time I can use just the <code>_id</code> fields is during the deserialization of the JSON. When this is the case, I only need to update the top-most object (<code>A</code> in my example) and some other trivial fields. In a perfect world, I can just throw some annotations on my class to solve this problem, but I haven't found anything capable of doing this.</p>
  
  <p>Is this even possible? If so, can it be done with JPA/Jackson annotations?</p>
  
  <p>Thanks!</p>