Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Wednesday, June 3, 2009

JPA annotation for Large Texts and Blobs(MySQL)

One of the columns in the entity had @Lob annotation type as had to store large amount of text.

DB used was MySQL 5.x

It threw the following error when the text was larger than 64KB.

java.sql.BatchUpdateException: Data truncation: Data too long for column 'data' at row 1
When I looked the table, it had the definition: "data VARCHAR(65535)"

We need to set the @Column annotation for size manually, if the size is more than 64K.

@Lob
@Column(name = "data", length = 1000000)
private String data;

When the table is created for this setting in MySQL, it has the definition "data MEDIUMTEXT" which can store approx 16MB of data!

In case you need to store binary data like images more than 64KB(but less than 16MB of course), "MEDIUMBLOB" is the choice in MySQL.

Java code for that will be:

@Lob
@Column(length = 2000000)
private byte[] image;