views:

75

answers:

1

I'm new to the hibernate world and I am using it to map a table that stores files of all types. I am however recieving a very strange error:

javax.servlet.ServletException: java.lang.ClassCastException: [B cannot be cast to java.sql.Blob

I have mapped my MySql LONGBLOB column has: <property name="fileData" type="blob" .../> and <property name="fileData" type="longblog" .../> but both don't work.

I'm currently using spring mvc version 3.x the latest version and tomcant 7 if that helps.

edit: here is how my POJO looks like for fileObject:

package com.kc.models;

public class FileObject {

private String fileName;
private String type;
private double size;
private byte[] file;
private int id;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getFileName() {
    return fileName;
}
public void setFileName(String fileName) {
    this.fileName = fileName;
}
public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}
public double getSize() {
    return size;
}
public void setSize(double size) {
    this.size = size;
}
public byte[] getFile() {
    return file;
}
public void setFile(byte[] file) {
    this.file = file;
}

}

And here is how my hbm.xml file looks like:

<class name="com.kc.models.FileObject" table="FILES">
    <id name="id" column="ID">
        <generator class="native" />
    </id>
    <property name="fileName" type="string" column="FILENAME" />
    <property name="type" type="string" column="TYPE" />
    <property name="size" type="double" column="SIZE" />
    <property name="file" type="blob" column="FILE" />
</class>

O and here is a print screen of mySql: http://img412.imageshack.us/img412/3663/fileobject.jpg

A: 

The exception message says that you are trying to cast a byte[] (represented as [B ) to a java.sql.Blob:

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

The problem seems to be that while you have defined the POJO property file as a byte[], you are mapping it as an `java.sql.Blob' at Hibernate mapping.

Try to change the property type at the POJO:

package com.kc.models;
public class FileObject {
    //...
    private java.sql.Blob file;
    //...
}
Tomas Narros
http://i-proving.ca/space/Technologies/Hibernate/Blobs+and+Hibernate - example of converting Blob to byte[] and byte[] to Blob.
MarrLiss
Can i still write byte arrays in a java.sql.Blob and does it have a limited size? the reason why i defined a longblob in mysql is so i can store large files and i have heard the default "blog" doesnt store much bytes.
jonney
@MarrLiss thanks for the link (+1)
Tomas Narros
@jonney: check the useful link provided by MarrLiss. There you'll find how you can handle the conversion between Blob an byte[]. The java.sql.Blob interface API doesn't define any size limit. http://download.oracle.com/javase/1.5.0/docs/api/java/sql/Blob.html
Tomas Narros
cool ok maybe i can just try using java.sql.blob instead of a byte array or copy and paste the code from the link but it says how if your ready a large file, it will affect your RAM.
jonney
I WILL REPORT BACK WITH RESULTS THANKS MAN
jonney
tried the example and i still recieved the same exception so i simply changed the file type from byte[] to Blog and now i am getting a different exception complaining that the data i am trying to add for field FILE is too long?? the files im trying to add are simple image files so none are larger than 2mb at best
jonney
Tomas Narros
yea that worked thanks
jonney
I'm glad to read this.
Tomas Narros