【问题标题】:JPA, Mysql Blob returns data too longJPA、Mysql Blob 返回数据过长
【发布时间】:2011-03-31 01:13:17
【问题描述】:

我的实体中有一些 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_allowedparam 设置为 16M - 我错过了什么吗?

【问题讨论】:

标签: java hibernate jpa blob


【解决方案1】:

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

  • TINYBLOB:最大长度为 255 个字节
  • BLOB:最大长度为 65,535 字节
  • MEDIUMBLOB:最大长度为 16,777,215 字节
  • LONGBLOB:最大长度为 4,294,967,295 字节

请注意,如果您从 JPA 注释生成表,则可以通过指定 Columnlength 属性来“控制” MySQL 将使用的类型,例如:

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

根据length,您将获得:

       0 < length <=      255  -->  `TINYBLOB`
     255 < length <=    65535  -->  `BLOB`
   65535 < length <= 16777215  -->  `MEDIUMBLOB`
16777215 < length <=    2³¹-1  -->  `LONGBLOB`

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      我在下面使用它,它适用于图像

      @Lob
      @Column(name = "file", columnDefinition = "LONGBLOB")
      private byte[] file;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-09
        • 2021-06-11
        • 1970-01-01
        • 2012-05-29
        • 1970-01-01
        • 1970-01-01
        • 2018-07-15
        • 1970-01-01
        相关资源
        最近更新 更多