title: error converting from string to blob in spring mvc hibernate application
body: <p>In a spring mvc application that utilizes hibernate and jpa, I have a form that needs to store a document including metadata about the document such as the current date when the form is submitted.  I have the model, controller, and jsp all set up, but when it runs, the create or add jsp is returned when the user clicks on the button to submit a document to the database.  The error indicates that the problem is that the code is not handling the conversion from String to Blob.  I HAVE NOTED THE LOCATION IN THE <code>processCreationForm()</code> METHOD WHERE THE CODE IS RETURNING THE <code>createOrUpdateDocumentForm.jsp</code> INSTEAD OF REDIRECTING TO THE LIST JSP AND I HAVE INCLUDED THE ERROR MESSAGE AT THE BOTTOM OF THIS POSTING.</p>
  
  <p><b>How can I change my code below so that document is saved to the database when the user clicks on the submit button?</b>  </p>
  
  <p>Here is my createOrUpdateDocumentForm.jsp:  </p>
  
  <pre><code>&lt;body&gt;
  &lt;script&gt;
      $(function () {
          $("#created").datepicker({ dateFormat: 'yy/mm/dd'});
      });
  &lt;/script&gt;
  &lt;div class="container"&gt;
      &lt;jsp:include page="../fragments/bodyHeader.jsp"/&gt;
      &lt;c:choose&gt;
          &lt;c:when test="${document['new']}"&gt;
              &lt;c:set var="method" value="post"/&gt;
          &lt;/c:when&gt;
          &lt;c:otherwise&gt;
              &lt;c:set var="method" value="put"/&gt;
          &lt;/c:otherwise&gt;
      &lt;/c:choose&gt;
  
      &lt;h2&gt;
          &lt;c:if test="${document['new']}"&gt;New &lt;/c:if&gt;
          Document
      &lt;/h2&gt;
  
      &lt;form:form modelAttribute="document" method="${method}"
                 class="form-horizontal"&gt;
          &lt;div class="control-group" id="patient"&gt;
              &lt;label class="control-label"&gt;Patient &lt;/label&gt;
  
              &lt;c:out value="${document.patient.firstName} ${document.patient.lastName}"/&gt;
          &lt;/div&gt;
          &lt;petclinic:inputField label="Name" name="name"/&gt;
          &lt;petclinic:inputField label="Description" name="description"/&gt;
          &lt;div class="control-group"&gt;
              &lt;petclinic:selectField name="type" label="Type " names="${types}" size="5"/&gt;
          &lt;/div&gt;
          &lt;td&gt;&lt;input type="file" name="content" id="content"&gt;&lt;/input&gt;&lt;/td&gt;
          &lt;div class="form-actions"&gt;
              &lt;c:choose&gt;
                  &lt;c:when test="${document['new']}"&gt;
                      &lt;button type="submit"&gt;Add Document&lt;/button&gt;
                  &lt;/c:when&gt;
                  &lt;c:otherwise&gt;
                      &lt;button type="submit"&gt;Update Document&lt;/button&gt;
                  &lt;/c:otherwise&gt;
              &lt;/c:choose&gt;
          &lt;/div&gt;
      &lt;/form:form&gt;
      &lt;c:if test="${!document['new']}"&gt;
      &lt;/c:if&gt;
      &lt;jsp:include page="../fragments/footer.jsp"/&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Here are the relevant parts of the controller:  </p>
  
  <pre><code>@RequestMapping(value = "/patients/{patientId}/documents/new", method = RequestMethod.GET)
  public String initCreationForm(@PathVariable("patientId") int patientId, Map&lt;String, Object&gt; model) {
      Patient patient = this.clinicService.findPatientById(patientId);
      Document document = new Document();
      patient.addDocument(document);
      model.put("document", document);
      return "documents/createOrUpdateDocumentForm";
  }
  
  @RequestMapping(value = "/patients/{patientId}/documents/new", method = RequestMethod.POST)
  public String processCreationForm(@ModelAttribute("document") Document document, BindingResult result, SessionStatus status) {
      document.setCreated();
      //THE FOLLOWING LINE PRINTS OUT A VALID DATE FOR document.getCreated()
      System.out.println("document.getCreated() is: "+document.getCreated());
      new DocumentValidator().validate(document, result);
      if (result.hasErrors()) {
          System.out.println("result.getFieldErrors() is: "+result.getFieldErrors());
          //THIS IS BEING RETURNED BECAUSE result.getFieldErrors() RETURNS WHAT IS BEING  
          //SHOWN AT THE BOTTOM OF THIS POSTING, BELOW  
          return "documents/createOrUpdateDocumentForm";
      }
      else {
          this.clinicService.saveDocument(document);
          status.setComplete();
          return "redirect:/patients?patientID={patientId}";
      }
  }
  </code></pre>
  
  <p>And here is the the model, which are parts of Document.java:  </p>
  
  <pre><code>@Entity
  @Table(name = "documents")
  public class Document {
      @Id
      @GeneratedValue
      @Column(name="id")
      private Integer id;
  
      @ManyToOne
      @JoinColumn(name = "client_id")
      private Patient patient;
  
      @ManyToOne
      @JoinColumn(name = "type_id")
      private DocumentType type;
  
      @Column(name="name")
      private String name;
  
      @Column(name="description")
      private String description;
  
      @Column(name="filename")
      private String filename;
  
      @Column(name="content")
      @Lob
      private Blob content;
  
      @Column(name="content_type")
      private String contentType;
  
      @Column(name = "created")
      private Date created;
  
      public Integer getId(){return id;}
      public void setId(Integer i){id=i;}
  
      protected void setPatient(Patient patient) {this.patient = patient;}
      public Patient getPatient(){return this.patient;}
  
      public void setType(DocumentType type) {this.type = type;}
      public DocumentType getType() {return this.type;}
  
      public String getName(){return name;}
      public void setName(String nm){name=nm;}
  
      public String getDescription(){return description;}
      public void setDescription(String desc){description=desc;}
  
      public String getFileName(){return filename;}
      public void setFileName(String fn){filename=fn;}
  
      public Blob getContent(){return content;}
      public void setContent(Blob ct){content=ct;}
  
      public String getContentType(){return contentType;}
      public void setContentType(String ctype){contentType=ctype;}
  
      public void setCreated(){created=new java.sql.Date(System.currentTimeMillis());}
      public Date getCreated() {return this.created;}
  
      @Override
      public String toString() {return this.getName();}
      public boolean isNew() {return (this.id == null);}
  
  }
  </code></pre>
  
  <p>The above code compiles, but when the user presses the submit button after entering the information to upload a document, the same add or update form is returned instead of redirecting to the summary page.  This indicates from the controller method above that result.haserrors is true even though none of the errors checked for by system.out.println are true.  The eclipse console does not show an error.  However result.getFieldErrors() prints out the following:  </p>
  
  <pre><code>[
  Field error in object 'document' on field 'content': 
  rejected value [mydocname.txt]; 
  codes [typeMismatch.document.content,typeMismatch.content,typeMismatch.java.sql.Blob,typeMismatch]; 
  arguments [org.springframework.context.support.DefaultMessageSourceResolvable: 
  codes [document.content,content]; 
  arguments []; default message [content]]; 
  default message 
  [
  Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Blob' for property 'content'; 
  nested exception is java.lang.IllegalStateException: 
  Cannot convert value of type [java.lang.String] to required type [java.sql.Blob] for property 'content': 
  no matching editors or conversion strategy found
  ]
  ]
  </code></pre>