JPA、Mysql Blob 返回数据太长

发布于 2024-09-14 17:06:56 字数 1805 浏览 1 评论 0原文

我的实体中有一些 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 - 我错过了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没结果 2024-09-21 17:06:56

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

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

请注意,如果您从 JPA 注释生成表,则可以通过指定该表的 length 属性来“控制”MySQL 将使用的类型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`

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`
悲喜皆因你 2024-09-21 17:06:56

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

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

I use below and it works for images

@Lob
@Column(name = "file", columnDefinition = "LONGBLOB")
private byte[] file;
佼人 2024-09-21 17:06:56

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

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

In our case we had to use the following syntax:

public class CcpArchive
{
    ...
    private byte[] ccpImage;
    ...
    @Lob
    @Column(nullable = false, name = "CCP_IMAGE", columnDefinition="BINARY(500000)")
    public byte[] getCcpImage()
    {
        return ccpImage;
    }
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文