title: How to save specific properties of a JPA entity model attribute from JSP page
body: <p>We are using JPA entity beans as our model for Spring MVC controller on a jsp page.  One of our jsp pages is a partial view of this entity which does not show all properties. Whenever we try to update our entity using the service layer from the controller only the properties used on the jsp form are persisted and all others are nulled out.  What is the correct way to handle this situation?  We do not want to have to specify hidden fields on the form.</p>
  
  <p>So in this case when the controller calls the service.update(client) method the name field will be null because it does not exist in the form.jsp.</p>
  
  <p><strong>form.jsp</strong></p>
  
  <pre><code>&lt;form:form modelAttribute="client" method="get" action="${action}"&gt;
  &lt;table width="100%"&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td valign="top"&gt;&lt;spring:message code="label.tradeOrderManagementSystem"/&gt;:&lt;/td&gt;
                      &lt;td&gt;
                          &lt;form:select path="tradeOrderManagementSystems" &gt;
                              &lt;form:options items="${tradeOrderManagementSystemList}" itemValue="id" itemLabel="name" /&gt;
                          &lt;/form:select&gt;
                          &lt;a href="&lt;spring:url value="/tradeOrderManagementSystem/add"/&gt;" class="addAndReturn"&gt;&lt;span&gt;&lt;spring:message code="add"/&gt;&lt;/span&gt;&lt;/a&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;form:errors path="tradeOrderManagementSystems" cssClass="errors" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
              &lt;/table&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  &lt;input type="hidden" name="submitted" value="true"&gt;
  </code></pre>
  
  <p>    </p>
  
  <p><strong>controller</strong></p>
  
  <pre><code>@RequestMapping("/{id}/edit")
  public ModelAndView edit(HttpServletRequest request,
          HttpServletResponse response,
          @ModelAttribute("client") Client client,
          BindingResult result,
          @PathVariable("id") int id,
          Model model) {
      ControllerContext ctx = new ControllerContext(request, response);
      init(ctx);
  
      setAdvancedSearchAvailable(ctx, true);
      buildShowAndEditVerticalMenu(ctx, id, false);
  
      if (id == 0) {
          result.addError(new ObjectError("client", getMessage("error.idNeeded")));
          return getModelAndView(ctx, "itEfficiencies/form");
      } else {
          if (!isSubmission(ctx)) {
              client = clientService.find(id);
              model.addAttribute("client", client);
              fillClientForm(model);
              return getModelAndView(ctx, "itEfficiencies/form");
          } else {
              //clientValidator.validate(client, result);
              if (result.hasErrors()) {
                  fillClientForm(model);
                  return getModelAndView(ctx, "itEfficiencies/form");
              } else {
                  try {
                      //checkClientProperties(client);
                      client.setId(id);
                      client = clientService.update(client);  //method updates only form fields and nulls out all others
                  } catch (Exception e) {
                      e.printStackTrace();
                      result.addError(new ObjectError("client", getMessage("error.save")));
                      fillClientForm(model);
                      return getModelAndView(ctx, "itEfficiencies/form");
                  }
                  return getModelAndView(ctx, "/staffingByClient/" + client.getId() + "/show", true);
              }
          }
      }
  }    
  </code></pre>
  
  <p><strong>Client.java</strong></p>
  
  <pre><code>@Entity
  public class Client extends AbstractEntity&lt;Integer&gt; {
  
  private static final long serialVersionUID = 1L;
  
  public static final String FIND_BY_NAME = "Client.FIND_BY_NAME";
  
  public static final String COUNT_BY_NAME = "Client.COUNT_BY_NAME";
  
  @Basic(optional = false)
  @Column(nullable = false, length = 125)
  private String name;
  
  @ManyToMany(fetch = FetchType.LAZY)
  @JoinTable(inverseJoinColumns = {
      @JoinColumn(name = "trade_order_management_system_id")}, uniqueConstraints =
  @UniqueConstraint(name = "UK_client_trade_order_mgmt_client_id_trade_order_mgmt_id",
  columnNames = {"client_id", "trade_order_management_system_id"}))
  @ForeignKey(name = "FK_client_trade_order_management_systems_client_id",
  inverseName = "FK_client_trade_order_mgmt_sys_trade_order_management_system_id")
  private List&lt;TradeOrderManagementSystem&gt; tradeOrderManagementSystems;
  
  public Client() {
  }
  
  public Client(Integer id) {
      this.id = id;
  }
  
  public Client(Integer id, String name) {
      this.id = id;
      this.name = name;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }   
  
  public List&lt;TradeOrderManagementSystem&gt; getTradeOrderManagementSystems() {
      return tradeOrderManagementSystems;
  }
  
  public void setTradeOrderManagementSystems(List&lt;TradeOrderManagementSystem&gt; tradeOrderManagementSystems) {
      this.tradeOrderManagementSystems = tradeOrderManagementSystems;
  }
  
  @Override
  public boolean equals(Object object) {
      // TODO: Warning - this method won't work in the case the id fields are not set
      if (!(object instanceof Client)) {
          return false;
      }
      Client other = (Client) object;
      if ((this.id == null &amp;&amp; other.id != null) || (this.id != null &amp;&amp; !this.id.equals(other.id))) {
          return false;
      }
      return true;
  }
  </code></pre>
  
  <p>}    </p>
  
  <p><strong>service methods</strong></p>
  
  <pre><code>public abstract class CrudService&lt;T, ID extends Serializable&gt; extends DAOImpl&lt;T, ID&gt; {
  
  /**
   * Updates an entity from an existing entity.
   *
   * @since 0.0.1
   * 
   * @param entity
   * @return the managed instance of the updated entity
   */
  @Override
  @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
  public T update(T entity, ID id) {
      return super.update(assignDefaultValues(entity), id);
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public abstract class DAOImpl&lt;T, ID extends Serializable&gt; implements DAO&lt;T, ID&gt; {
  
  private Class&lt;T&gt; persistentClass;
  
  @PersistenceContext(unitName = "krfsPersistenceUnit")
  protected EntityManager entityManager;
  
  /**
   * Instantiates an instance of this class and sets the &lt;code&gt;persistentClass&lt;/code&gt;
   * based on the identifier type
   *
   * @since 0.0.1
   */
  public DAOImpl() {
      this.persistentClass = (Class&lt;T&gt;) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
  }
  
  /**
   * @since 0.0.1
   * 
   * @return the type to be persisted
   */
  @Override
  public Class&lt;T&gt; getPersistentClass() {
      return persistentClass;
  }
  
  /**
   * Updates an entity from an existing entity.
   *
   * @since 0.0.1
   * 
   * @param entity
   * @param id the identifier of the entity
   * 
   * @return the managed instance of the updated entity
   */
  @Override
  public T update(T entity, ID id) {
      //Find a managed instance of the entity first and copy the properties
      //to the passed in entity before merging.  This ensures that entityManager
      //will not create a new entity with merge.
      Object ref = this.entityManager.getReference(persistentClass, id);
      if (ref != null) {
          BeanUtils.copyProperties(entity, ref);
      }
      return (T) this.entityManager.merge(ref);
  }
  </code></pre>
  
  <p>}    </p>