JPA,Mysql Blob返回的数据太长 [英] JPA, Mysql Blob returns data too long

查看:121
本文介绍了JPA,Mysql Blob返回的数据太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实体中有一些 byte [] 字段,例如:

  @Entity 
public class ServicePicture实现Serializable {
private static final long serialVersionUID = 2877629751219730559L;
// seam-gen属性(你应该编辑这些)
@Id
@GeneratedValue
private Long id;
私有字符串描述;

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

在我的数据库模式中,字段设置为 BLOB 所以这应该没问题。无论如何:每次当我尝试插入一张图片或PDF - 没有比 1mb 大的东西时,我只收到这个



<$ p $ J $ {code> 16:52:27,327 WARN [JDBCExceptionReporter] SQL错误:0,SQLState:22001
16:52:27,327错误[JDBCExceptionReporter]数据截断:在第1行
16:52:27,328错误[STDERR] javax.persistence.PersistenceException:org.hibernate.exception.DataException:无法插入:[de.ac.dmg.productfinder.entity.ServicePicture]
16:52:27,328错误[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错误[STDERR]在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
16:52:27,328错误[sunder.reflect]中的[STDERR] NativeMethodAccessorImpl.invoke(未知来源)
16:52:27,328 E RROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
16:52:27,328错误[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)

我检查了我的MySQL cnf和 max_allowed param设置为 16M - 我是否缺少某些东西?

解决方案

取决于用于图片列的列类型。根据您的需要,使用:


  • TINYBLOB :最大长度为255字节

  • BLOB :最大长度为65,535字节
  • MEDIUMBLOB :最大长度为16,777,215字节

  • LONGBLOB :最大长度为4,294,967,295字节 $ b $注意,如果你从JPA注解生成你的表,你可以通过指定长度来控制MySQL将使用的类型 code> Column 的code>属性,例如:

    pre $ @Lob @Basic(fetch = FetchType.LAZY)
    @Column(长度= 100000)
    私人字节[]图片;

    根据长度,你会get:

      0 <长度< = 255  - > `TINYBLOB` 
    255<长度<= 65535 - > `BLOB`
    65535<长度<= 16777215 - > `MEDIUMBLOB`
    16777215<长度< = 2 31-1 - > `LONGBLOB`


    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?

    解决方案

    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`
    

    这篇关于JPA,Mysql Blob返回的数据太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆