JPA,Mysql Blob返回的数据太长

时间:2010-08-17 15:00:53

标签: java hibernate jpa blob

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

3 个答案:

答案 0 :(得分:116)

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

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

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

Column

根据@Lob @Basic(fetch = FetchType.LAZY) @Column(length=100000) private byte[] picture; ,您将获得:

length

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

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