I got the following error
Caused by: org.hibernate.HibernateException: Wrong column type in TestTable for column PAYLOAD. Found: blob, expected: tinyblob
at org.hibernate.mapping.Table.validateColumns(Table.java:284)
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1174)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:139)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:387)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:883)
... 60 more
The column hibernate is complaining about is declared as
private byte[] messagePayload;
@Column(name="PAYLOAD")
public byte[] getMessagePayload() {
return messagePayload;
}
public void setMessagePayload(byte[] messagePayload) {
this.messagePayload = messagePayload;
}
The table in the MySQL table is declared as a BLOB type. Why doesnt Hibernate want to map to it and why does it insist that i use TINYBLOB?
Thanks
A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types are TINYBLOB , BLOB , MEDIUMBLOB , and LONGBLOB . These differ only in the maximum length of the values they can hold. The four TEXT types are TINYTEXT , TEXT , MEDIUMTEXT , and LONGTEXT .
@javax. persistence. Lob signifies that the annotated field should be represented as BLOB (binary data) in the DataBase. You can annotate any Serializable data type with this annotation. In JPA, upon persisting (retrieval) the field content will be serialized (deserialized) using standard Java serialization.
LOB or Large OBject refers to a variable length datatype for storing large objects. The datatype has two variants: CLOB – Character Large Object will store large text data. BLOB – Binary Large Object is for storing binary data like image, audio, or video.
columnDefinition definition: The SQL fragment that is used when generating the DDL for the column.
You could try to set explicitly the type blob with columnDefinition attribute. Like this:
@Column(name="PAYLOAD",columnDefinition="blob")
Or use the @Lob annotation:
@Column(name="PAYLOAD")
@Lob(type = LobType.BLOB)
@Column(columnDefinition="blob")
Did not work for me. My specifications: - JPA - Hibernate - MySQL
Solution:
ALTER TABLE `my_table_name` CHANGE `my_column` `my_column` LONGBLOB default NULL;
public MyClass {
@Lob
@Column(length=100000)
private byte[] myBlob;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With