Java JPA,Mysql Blob 返回数据太长

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3503841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:22:08  来源:igfitidea点击:

JPA, Mysql Blob returns data too long

javahibernatejpablob

提问by onigunn

I've got some byte[]fields in my entities, e.g.:

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;

On my database schema the field is set to BLOBso this should be fine. Anyway: Everytime when I try to insert a picture or pdf - nothing bigger than 1mb, I only recieve this

在我的数据库模式中,该字段设置为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)

I've checked my MySQL cnf and the max_allowedparam is set to 16M- am I missing something?

我检查了我的 MySQL cnf 并且max_allowed参数设置为16M- 我错过了什么吗?

采纳答案by Pascal Thivent

It all depends on the column type used for the picturecolumn. Depending on your needs, use a:

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

  • 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
  • TINYBLOB: 最大长度 255 字节
  • BLOB: 最大长度为 65,535 字节
  • MEDIUMBLOB: 最大长度 16,777,215 字节
  • LONGBLOB: 最大长度 4,294,967,295 字节

Note that if you generate your table from the JPA annotations, you can "control" the type MySQL will use by specifying the lengthattribute of the Column, for example:

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

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

Depending on the length, you'll get:

根据length,您将获得:

       0 < length <=      255  -->  `TINYBLOB`
     255 < length <=    65535  -->  `BLOB`
   65535 < length <= 16777215  -->  `MEDIUMBLOB`
16777215 < length <=    231-1  -->  `LONGBLOB`

回答by kory

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;
    }
    ...
}

回答by Cyber

I use below and it works for images

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

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