J'en ai byte[]
champs dans mes entités, par exemple:
@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;
Sur mon schéma de base de données, le champ est défini sur BLOB
, cela devrait donc être correct. Quoi qu'il en soit: chaque fois que j'essaie d'insérer une image ou un pdf - rien de plus grand que 1mb
, Je ne reçois que cela
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)
J'ai vérifié mon cnf MySQL et le max_allowed
param est défini sur 16M
- est-ce que je manque quelque chose?
Tout dépend du type de colonne utilisé pour la colonne picture
. Selon vos besoins, utilisez:
TINYBLOB
: longueur maximale de 255 octetsBLOB
: longueur maximale de 65 535 octetsMEDIUMBLOB
: longueur maximale de 16 777 215 octetsLONGBLOB
: longueur maximale de 4 294 967 295 octetsNotez que si vous générez votre table à partir des annotations JPA, vous pouvez "contrôler" le type que MySQL utilisera en spécifiant l'attribut length
du Column
, par exemple:
@Lob @Basic(fetch = FetchType.LAZY)
@Column(length=100000)
private byte[] picture;
Selon le length
, vous obtiendrez:
0 < length <= 255 --> `TINYBLOB`
255 < length <= 65535 --> `BLOB`
65535 < length <= 16777215 --> `MEDIUMBLOB`
16777215 < length <= 2³¹-1 --> `LONGBLOB`
Dans notre cas, nous avons dû utiliser la syntaxe suivante:
public class CcpArchive
{
...
private byte[] ccpImage;
...
@Lob
@Column(nullable = false, name = "CCP_IMAGE", columnDefinition="BINARY(500000)")
public byte[] getCcpImage()
{
return ccpImage;
}
...
}