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:
@Entity(name = "blob")
public class Blob {
...
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "blobImg", nullable = false)
private byte[] blobImg;
}
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:
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
I've tried to change the field definition with several approaches I've found on the web:
Approach 1:
@Column(name = "blobImg", nullable = false, columnDefinition = "BINARY(256)", length = 256)
private byte[] blobImg;
Approach 2:
@Lob
@Column(name="blobImg", columnDefinition="bytea")
private byte[] blobImg;
Approach 3: Defining an hibernate mapping on blob.hbm.xml file and refering it from entityManagerFactory bean:
<?xml version='1.0' encoding='UTF-8'?>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="mappingResources">
<list>
<value>blob.hbm.xml</value>
</list>
</property>
</bean>
blob.hbm.xml mapping:
<hibernate-mapping>
<class name="guiatv.persistence.domain.Blob" table="blob">
<property name="blobImgProperty">
<column name="blobImg" sql-type="binary"></column>
</property>
</class>
</hibernate-mapping>
Approach 4: changing blob.hbm.xml mapping to the following:
<?xml version='1.0' encoding='UTF-8'?>
<hibernate-mapping>
<class name="guiatv.persistence.domain.Blob" table="blob">
<property name="blobImg" type="binary">
<column name="blobImg" />
</property>
</class>
</hibernate-mapping>
All of them are throwing the same exception.
How can I solve it? Thank you!