Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cant JPA/hibernate map to MySQL blob type?

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

like image 826
ziggy Avatar asked Feb 15 '12 18:02

ziggy


People also ask

What is blob type in mysql?

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 .

What is @LOB in JPA?

@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.

How do you specify large objects in hibernate?

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.

What is columnDefinition in hibernate?

columnDefinition definition: The SQL fragment that is used when generating the DDL for the column.


2 Answers

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)
like image 87
bruno.zambiazi Avatar answered Oct 15 '22 16:10

bruno.zambiazi


@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;
}         
like image 21
Fico Avatar answered Oct 15 '22 18:10

Fico