title: Spring form binding issue with nested object empty fields
body: <p>I am having rather an awkward issue with spring @ModelAttribute form binding. I have the following Entity class which is used in form binding as a nested class of Policy class.</p>
  
  <pre><code>@javax.persistence.Entity
  @Table(name = "entity")
  @XmlRootElement
  public class Entity implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "entity_id")
      private Integer entityId;
  
      @Basic(optional = false)
      @NotNull
      @Column(name = "entity_type")
      private int entityType;
  
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 2147483647)
      @Column(name = "entity_name")
      private String entityName;
  
      public Entity() {
      }
  
      public Entity(Integer entityId) {
          this.entityId = entityId;
      }
  
      public Entity(Integer entityId, int entityType, String entityName) {
          this.entityId = entityId;
          this.entityType = entityType;
          this.entityName = entityName;
      }
  
      public Integer getEntityId() {
          return entityId;
      }
  
      public void setEntityId(Integer entityId) {
          this.entityId = entityId;
      }
  
      public int getEntityType() {
          return entityType;
      }
  
      public void setEntityType(int entityType) {
          this.entityType = entityType;
      }
  
      public String getEntityName() {
          return entityName;
      }
  
      public void setEntityName(String entityName) {
          this.entityName = entityName;
      }
  }
  </code></pre>
  
  <p>And the main interest of my problem, Policy class, which is the top level class and main binding is as follows:</p>
  
  <pre><code>@Entity
  @Table(name = "policy")
  @XmlRootElement
  public class Policy implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "policy_id")
      private Integer policyId;
  
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 2147483647)
      @Column(name = "reference_number")
      private String referenceNumber;
  
      @JoinColumn(name = "adjuster_id", referencedColumnName = "entity_id", nullable = true)
      @ManyToOne(optional = true)
      private Entity adjusterId;
  
      public Policy() {
      }
  
      public Policy(Integer policyId) {
          this.policyId = policyId;
      }
  
      public Integer getPolicyId() {
          return policyId;
      }
  
      public void setPolicyId(Integer policyId) {
          this.policyId = policyId;
      }
  
      public String getReferenceNumber() {
          return referenceNumber;
      }
  
      public void setReferenceNumber(String referenceNumber) {
          this.referenceNumber = referenceNumber;
      }
  
  
      public Entity getAdjusterId() {
          return adjusterId;
      }
  
      public void setAdjusterId(Entity adjusterId) {
          this.adjusterId = adjusterId;
      }
  }
  </code></pre>
  
  <p>And this is the controller method which takes in a ModelAttribute annotated Policy parameter from view binding.</p>
  
  <pre><code>@RequestMapping(value = "/create", method = RequestMethod.POST)
      public ModelAndView create(@ModelAttribute Policy p) {
          policyService.create(p);
          return new ModelAndView("viewName");
      }
  </code></pre>
  
  <p>Finally this is the form/view part of the code:</p>
  
  <pre><code>&lt;form id="policyInformationForm" role="form" th:object="${policy}" th:action="@{${mode == 'CREATE'} ? '/policy/create' : '/policy/update'}" method="post"&gt;
                          &lt;div class="box-body"&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;label for="policyId"&gt;Policy Id&lt;/label&gt;
                                  &lt;input type="text" th:field="*{policyId}" class="form-control" id="policyId" placeholder="" readonly="readonly" /&gt;
                              &lt;/div&gt;
  
                              &lt;div class="form-group"&gt;
                                  &lt;label for="referenceNumber"&gt;Policy Number (May change while saving!!!)&lt;/label&gt;
                                  &lt;input type="text" th:field="*{referenceNumber}" class="form-control" id="referenceNumber" placeholder="Enter Policy Number" th:readonly="${mode == 'UPDATE'}" /&gt;
                              &lt;/div&gt;
  
                              &lt;div class="form-group"&gt;
                                  &lt;label for="adjusterName"&gt;Adjuster&lt;/label&gt;
                                  &lt;div class="input-group"&gt;
                                      &lt;input type="hidden" th:field="*{adjusterId.entityId}" class="form-control" id="adjusterId" /&gt;
                                      &lt;input type="text" th:field="*{adjusterId.entityName}" class="form-control" id="adjusterName" placeholder="Enter Adjuster" /&gt;
                                      &lt;div class="input-group-btn"&gt;
                                          &lt;button id="adjusterSearchBtn" type="button" class="btn btn-success"&gt;&lt;span class="glyphicon glyphicon-search"&gt;&lt;/span&gt;&lt;/button&gt;
                                          &lt;button id="adjusterCreateBtn" type="button" class="btn btn-success"&gt;&lt;span class="glyphicon glyphicon-plus"&gt;&lt;/span&gt;&lt;/button&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
  
                          &lt;/div&gt;
  
                          &lt;div class="box-footer"&gt;
                              &lt;button type="submit" class="btn btn-primary"&gt;Submit&lt;/button&gt;
                              &lt;a th:href="@{'/policy/list'}" class="btn btn-warning"&gt;Cancel&lt;/a&gt;
                          &lt;/div&gt;
                      &lt;/form&gt;
  </code></pre>
  
  <p>The problem is when i submit the form; spring form binding binds the form parameters to respective fields of the Policy class but if the adjuster is not selected, namely hidden field of adjusterId value is blank (which is perfectly fine regarding the application) then spring instantiates a new Entity class with all fields having null values. This creates a problem on JPA persistence part of the application because of ManyToOne relation and JPA validations.</p>
  
  <p>Any pointers on how to overcome this problem. If the adjusterId from the view (form hidden input field) is null; then form binding should not instantiate a new Entity class with null field values. Instead it should set the adjusterId property of the Policy class as null.</p>
  
  <p>By the way i already gone through many of the similar questions, which in turn most of them are not relevant. One of them is exactly the <a href="https://stackoverflow.com/questions/34722091/spring-mvc-and-form-binding-how-to-tell-spring-not-to-instantiate-child-object">same question which is unanswered</a></p>
  
  <p>Thanks in advance...</p>