代码之家  ›  专栏  ›  技术社区  ›  onigunn

JPA,Mysql Blob返回的数据太长

  •  50
  • onigunn  · 技术社区  · 15 年前

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

    @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;
    

    在我的数据库架构中,字段设置为 BLOB 所以这应该没问题。不管怎样:每次我尝试插入图片或pdf时-没有比 1mb

    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)
    

    我已经检查了我的MySQL cnf和 max_allowed 参数设置为 16M

    2 回复  |  直到 15 年前
        1
  •  124
  •   Pascal Thivent    15 年前

    这完全取决于用于 picture 列。根据您的需要,使用:

    • TINYBLOB
    • BLOB :最大长度为65535字节
    • MEDIUMBLOB
    • LONGBLOB :最大长度为4294967295字节

    注意,如果从JPA注释生成表,那么可以通过指定 length 的属性 Column

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

    取决于 长度 ,您将得到:

           0 < length <=      255  -->  `TINYBLOB`
         255 < length <=    65535  -->  `BLOB`
       65535 < length <= 16777215  -->  `MEDIUMBLOB`
    16777215 < length <=    2³¹-1  -->  `LONGBLOB`
    
        2
  •  2
  •   Cyber    5 年前

    我使用下面的图片,它的作品

    @Lob
    @Column(name = "file", columnDefinition = "LONGBLOB")
    private byte[] file;
    
        3
  •  1
  •   kory    10 年前

    在本例中,我们必须使用以下语法:

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