Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

java - JPA, Mysql Blob returns data too long

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_allowedparam is set to 16M - am I missing something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 <=    231-1  -->  `LONGBLOB`

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...