4

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.

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

2 Comments

ok, thanks. Is there any other way to persist a long[] without having to "convert" it to a byte[] or something manually?
Please check the answer, I've added a link with one example from the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.