title: Store byte array in MySQL with Hibernate
body: <p>I'm trying to save an entity with a byte array field. I'm using Hibernate and JPA on top of a MySQL database.
  This is the field definiton, which worked fine for an embedded H2 database:</p>
  
  <pre><code>@Entity(name = "blob")
  public class Blob {
      ...
      @Lob
      @Basic(fetch = FetchType.LAZY)
      @Column(name = "blobImg", nullable = false)
      private byte[] blobImg;
  }
  </code></pre>
  
  <p>Now, with MySQL database, an exception is thrown everytime I execute blobRepository.save(). Actually, is may be thrown when Hibernate tries to autocreate the table of Blob entity. The exception is the following:</p>
  
  <blockquote>
    <p>o.h.engine.jdbc.spi.SqlExceptionHelper   : You have an error in your
    SQL syntax; check the manual that corresponds to your MySQL server
    version for the right syntax to use near 'blob (blobCols, blobImg,
    blobRows, channel, idBlobPersistence) values (50, _bina' at line 1</p>
  </blockquote>
  
  <p>I've tried to change the field definition with several approaches I've found on the web:</p>
  
  <p>Approach 1:</p>
  
  <pre><code>@Column(name = "blobImg", nullable = false, columnDefinition = "BINARY(256)", length = 256)
  private byte[] blobImg;
  </code></pre>
  
  <p>Approach 2:</p>
  
  <pre><code>@Lob
  @Column(name="blobImg", columnDefinition="bytea")
  private byte[] blobImg;
  </code></pre>
  
  <p>Approach 3:
  Defining an hibernate mapping on blob.hbm.xml file and refering it from entityManagerFactory bean:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="mappingResources"&gt;  
          &lt;list&gt;  
              &lt;value&gt;blob.hbm.xml&lt;/value&gt;  
          &lt;/list&gt;  
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>blob.hbm.xml mapping:</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="guiatv.persistence.domain.Blob" table="blob"&gt;
          &lt;property name="blobImgProperty"&gt;
              &lt;column name="blobImg" sql-type="binary"&gt;&lt;/column&gt;
          &lt;/property&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Approach 4:
  changing blob.hbm.xml mapping to the following:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;hibernate-mapping&gt;
      &lt;class name="guiatv.persistence.domain.Blob" table="blob"&gt;
          &lt;property name="blobImg" type="binary"&gt;
              &lt;column name="blobImg" /&gt;
          &lt;/property&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>All of them are throwing the same exception.</p>
  
  <p>How can I solve it?
  Thank you!</p>