title: How can I update a BLOB from an InputStream via an EntityManager?
body: <p>I have a table:</p>
  
  <pre><code>mysql&gt; desc documents;
  +---------+--------------+------+-----+---------------------+----------------+
  | Field   | Type         | Null | Key | Default             | Extra          |
  +---------+--------------+------+-----+---------------------+----------------+
  | id      | int(11)      | NO   | PRI | NULL                | auto_increment |
  | item_id | int(11)      | YES  | MUL | NULL                |                |
  | doctype | int(11)      | YES  | MUL | NULL                |                |
  | name    | varchar(128) | YES  | MUL | NULL                |                |
  | descr   | text         | YES  |     | NULL                |                |
  | created | timestamp    | NO   |     | CURRENT_TIMESTAMP   |                |
  | changed | timestamp    | NO   |     | 0000-00-00 00:00:00 |                |
  | doc     | longblob     | YES  |     | NULL                |                |
  +---------+--------------+------+-----+---------------------+----------------+
  </code></pre>
  
  <p>Which I have mapped as an <code>Entity</code> like this:</p>
  
  <pre><code>@Entity
  @Table(name = "documents", schema = "office_db")
  @XmlRootElement
  @NamedQueries({
      ...,
      @NamedQuery(name = "Documents.updateDocById", query = "UPDATE Documents d SET d.doc = :document WHERE d.id = :id")
  })
  public class Documents implements Serializable {
  
      @Column(name = "name", length = 128)
      private String name;
      @Lob
      @Column(name = "descr", length = 65535)
      private String descr;
      @Lob
      @Column(name = "doc")
      private byte[] doc;
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "id")
      private Integer id;
      @Basic(optional = false)
      @Column(name = "created")
      @Temporal(TemporalType.TIMESTAMP)
      private Date created;
      @Basic(optional = false)
      @Column(name = "changed")
      @Temporal(TemporalType.TIMESTAMP)
      private Date changed;
      ...
  }
  </code></pre>
  
  <p>And I want to update the LONGBLOB DOC column with the contents of a file. To that end, I have acquired an <code>InputStream</code> and want to associate it with my <code>NamedQuery</code> - I have naively tried the following, expecting it to fail (which it does, of course):</p>
  
  <pre><code>public String updateDocument(Integer id,InputStream is){
      String msg="";
      try{
          Query stmt=em.createNamedQuery("Documents.updateDocById")
                  .setParameter("document",is);
          stmt.executeUpdate();
      }
      ...
  }
  </code></pre>
  
  <p>If I had used a <code>PreparedStatement</code>, I could have used the <code>setBinaryStream</code> method, but is that really the right way?</p>