JPA, Mysql Blob returns data too long JPA, Mysql Blob returns data too long java java

JPA, Mysql Blob returns data too long


It all depends on the column type used for the picture column. Depending on your needs, use a:

  • TINYBLOB: maximum length of 255 bytes
  • BLOB: maximum length of 65,535 bytes
  • MEDIUMBLOB: maximum length of 16,777,215 bytes
  • LONGBLOB: maximum length of 4,294,967,295 bytes

Note that if you generate your table from the JPA annotations, you can "control" the type MySQL will use by specifying the length attribute of the Column, for example:

@Lob @Basic(fetch = FetchType.LAZY)@Column(length=100000)private byte[] picture;

Depending on the length, you'll get:

       0 < length <=      255  -->  `TINYBLOB`     255 < length <=    65535  -->  `BLOB`   65535 < length <= 16777215  -->  `MEDIUMBLOB`16777215 < length <=    2³¹-1  -->  `LONGBLOB`


I use below and it works for images

@Lob@Column(name = "file", columnDefinition = "LONGBLOB")private byte[] file;


In our case we had to use the following syntax:

public class CcpArchive{    ...    private byte[] ccpImage;    ...    @Lob    @Column(nullable = false, name = "CCP_IMAGE", columnDefinition="BINARY(500000)")    public byte[] getCcpImage()    {        return ccpImage;    }    ...}