I'm using MySQL with Hibernate to save uploaded files in the database. The file is transformed into a byte array as a field of a simple entity:
@Entity
@Table(name = "TestBlob")
public class TestBlob {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Lob
@Column(name = "file")
private byte[] file;
public byte[] getFile() {
return file;
}
public void setFile(byte[] file) {
this.file = file;
}
}
Then when I try to run the following code:
@Override
public void addBlob(TestBlob t) {
System.out.println("Trying to upload file with size:" + t.getFile().length);
Session session = sessionFactory.getCurrentSession();
session.save(t);
}
@Override
public void updateBlob(TestBlob t) {
System.out.println("Trying to upload file with size:" + t.getFile().length);
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(t);
}
I get this output:
Trying to upload file with size:8663040
Hibernate:
insert
into
TestBlob
(file)
values
(?)
...
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (17326125 > 16777216).
The only relevant post I've found is Loading 26MB text data from database consume JVM heap of 258MB or posts related to Postgre. Is there anything I can fix about my code or is it a some weird quirk that I need to know?
EDIT: I'm required to save files in a blob.