2010-08-17 26 views
45

Tengo algunos campos en mis byte[] entidades, por ejemplo .:APP, MySQL devuelve datos Blob demasiado tiempo

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

En mi esquema de la base del campo se establece en BLOB por lo que este debe estar bien. De todos modos: Cada vez cuando trato de insertar una imagen o pdf - nada más grande que 1mb, lo único que recibo este

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) 

He comprobado mi MySQL CNF y el max_allowed parámetro se fija a 16M - me estoy perdiendo algo?

+0

Resuelto por este enlace http://enricogi.blogspot.com/2008/12/blob-type-in-mysql.html - en resumen - Blob de MySQL tiene un tamaño máximo de 64k – onigunn

Respuesta

99

Todo depende del tipo de columna utilizado para la columna picture. Dependiendo de sus necesidades, utilizar un:

  • TINYBLOB: longitud máxima de 255 bytes
  • BLOB: longitud máxima de 65.535 bytes
  • MEDIUMBLOB: longitud máxima de 16.777.215 bytes
  • LONGBLOB: longitud máxima de 4294967295 bytes

Tenga en cuenta que si genera su tabla a partir de las anotaciones JPA, puede "controlar" el tipo MySQL wi ll uso especificando el atributo length del Column, por ejemplo:

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

Dependiendo del length, obtendrá:

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

Perf ect respuesta, gracias. –

1

En nuestro caso tuvimos que utilizar la siguiente sintaxis:

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