I've got some byte[]
fields in my entities, e.g.:
@Entity public class ServicePicture implements Serializable { private static final long serialVersionUID = 2877629751219730559L; // seam-gen attributes (you should probably edit these) @Id @GeneratedValue private Long id; private String description; @Lob @Basic(fetch = FetchType.LAZY) private byte[] picture;
On my database schema the field is set to BLOB
so this should be fine. Anyway: Everytime when I try to insert a picture or pdf - nothing bigger than 1mb
, I only recieve this
16:52:27,327 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: 22001 16:52:27,327 ERROR [JDBCExceptionReporter] Data truncation: Data too long for column 'picture' at row 1 16:52:27,328 ERROR [STDERR] javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not insert: [de.ac.dmg.productfinder.entity.ServicePicture] 16:52:27,328 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629) 16:52:27,328 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218) 16:52:27,328 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 16:52:27,328 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 16:52:27,328 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 16:52:27,328 ERROR [STDERR] at java.lang.reflect.Method.invoke(Unknown Source) 16:52:27,328 ERROR [STDERR] at org.jboss.seam.persistence.EntityManagerInvocationHandler.invoke(EntityManagerInvocationHandler.java:46) 16:52:27,328 ERROR [STDERR] at $Proxy142.persist(Unknown Source)
I've checked my MySQL cnf and the max_allowed
param is set to 16M
- am I missing something?
BLOB: Can handle up to 65,535 bytes of data. MEDIUMBLOB: The maximum length supported is 16,777,215 bytes. LONGBLOB: Stores up to 4,294,967,295 bytes of data.
Default. A BLOB without a specified length is defaulted to two gigabytes (2,147,483,647).
LONGBLOB: A binary large object column with a maximum length of 4294967295 (2^32 - 1) bytes, or 4GB in storage. Each LONGBLOB value is stored using a four-byte length prefix that indicates the number of bytes in the value.
Each size of blob field reserves extra bytes to hold size information. A longblob uses 4+n bytes of storage, where n is the actual size of the blob you're storing. If you're only ever storing (say) 10 bytes of blob data, you'd be using up 14 bytes of space.
It all depends on the column type used for the picture
column. Depending on your needs, use a:
TINYBLOB
: maximum length of 255 bytesBLOB
: maximum length of 65,535 bytesMEDIUMBLOB
: maximum length of 16,777,215 bytesLONGBLOB
: maximum length of 4,294,967,295 bytesNote 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;
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