开发者

Using longblob in hibernate

开发者 https://www.devze.com 2023-01-16 12:53 出处:网络
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:

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


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;
    //...
}


Try making sure you use @Access(FIELD) for some odd reason running on the JaveSE environment (i.e. JUnit test) and JaveEE environment (e.g. Glassfish) I get different results but it was made consistent when I added the @Access annotation.


public Blob getFile() {
    return Hibernate.createBlob(file);
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消