JPA "cannot be cast to java.sql.Blob"

11,606

You need to map the property as a byte[], not as long[].

The documentation says

@Lob indicates that the property should be persisted in a Blob or a Clob depending on the property type: java.sql.Clob, Character[], char[] and java.lang.String will be persisted in a Clob. java.sql.Blob, Byte[], byte[] and serializable type will be persisted in a Blob.

If the property type implements java.io.Serializable and is not a basic type, and if the property is not annotated with @Lob, then the Hibernate serializable type is used.

If you want to persist the array, you'll need to build a custom user type to transform the data type. You can find an example here http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#d0e2794

Share:
11,606
suicide
Author by

suicide

Updated on June 04, 2022

Comments

  • suicide
    suicide almost 2 years

    I'm using JPA2 with hibernate 3.6.1. and a Derby database and I used the following annotation for a blob:

    @Column(length = Integer.MAX_VALUE)
    @Lob
    long[] bucket;
    

    Hibernate creates the correct blob column but if I try to save an entity I get the following exception:

    java.lang.ClassCastException: [J cannot be cast to java.sql.Blob

    why and how can I make this work?

    If I annotate it without the @Lob I get a "Varchar for bit data" column which can only contain up to 32m.