开发者

java.lang.ClassCastException: CommonsMultipartFile cannot be cast to oracle.sql.BFILE

开发者 https://www.devze.com 2023-03-28 19:42 出处:网络
I am working on a screen which will upload a file to oracle table as BFILE type. I am using spring3 and hibernate3.

I am working on a screen which will upload a file to oracle table as BFILE type. I am using spring3 and hibernate3.

The BO class look like:

@Entity
@Table(name="abc_manuals")
public class ManualBo implements Serializable {

/* Persistent Fields */
@Id
@Column(name="id", nullable=false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long  mId;


@Column(name="guide")
@Type(type="com.bo.entity.BFILEType")
private BFILE guide;


public Long getMlId() {
    return mlId;
}
public void setMId(Long manualId) {
    this.mId = mId;
}

public BFILE getGuide() {
    return guide;
}
public void setGuide(BFILE guide) {
    guide = guide;
}

}

I have defined a BFILE userType:

public class BFILEType implements UserType, Serializable {

@SuppressWarnings("unused")
private static final String MARK_EMPTY = "<EmptyString/>";
private static final int[] TYPES = { OracleTypes.BFILE  };

public int[] sqlTypes() {
    return TYPES;
}

@SuppressWarnings("rawtypes")
public Class returnedClass() {
    return BFILE.class;
}

public boolean equals(Object x, Object y) {
    if (x==y) return true;
    if (x==null || y==null) return false;
    return x.equals(y);
}

public Object deepCopy(Object x) {
    return x;
}

public boolean isMu开发者_StackOverflowtable() { return false; }

   public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws 
   HibernateException, SQLException {
    BFILE bfile = (BFILE)rs.getObject(names[0]);
    return bfile;
}

public void nullSafeSet(PreparedStatement st, Object value, int index) throws 
     HibernateException, SQLException {
    if(value==null)
        st.setObject(index, null);
    else
        st.setObject(index, value, OracleTypes.BFILE);
}

public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
    return deepCopy(arg0);
}

public Serializable disassemble(Object value) {
    return (Serializable) deepCopy(value);
}

public int hashCode(Object arg0) throws HibernateException {
    return arg0.hashCode();
}

public Object replace(Object arg0, Object arg1, Object arg2) throws
     HibernateException {
    return deepCopy(arg0);
}

}

Problem is when I am trying to setFile in controller

manual.setGuide((oracle.sql.BFILE) form.getFile());

it is compiling well but when I upload file from screen it is giving following exception

java.lang.ClassCastException: org.springframework.web.multipart.commons.CommonsMultipartFile cannot be cast to oracle.sql.BFILE

How to solve this?

Solved :

I tried :

public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { if (value == null) { st.setObject(index, null); } else {

        OracleConnection oc = (OracleConnection) st.getConnection();
        OraclePreparedStatement opst = (OraclePreparedStatement) st;

        OracleCallableStatement ocs = (OracleCallableStatement) oc.prepareCall("{? = call BFILENAME('" + directory + "', '"+ filename + "')}");

        ocs.registerOutParameter(1, OracleTypes.BFILE);
        ocs.execute();
        BFILE bfile = ocs.getBFILE(1);
        opst.setBFILE(index, bfile);
    } 
}                                                                      

and its working fine.

Thanks!


java.lang.ClassCastException: org.springframework.web.multipart.commons.CommonsMultipartFile cannot be cast to oracle.sql.BFILE

That is correct, the two classes don't share a type hierarchy, there's no way to cast one to the other. Instead of casting the types, you should concentrate on transferring the contents.

This should be the code you need:

BFILE bfile = new BFILE();
bfile.setBytes(form.getFile().getBytes());
manual.setGuide(bfile);

UPDATE: It turns out it's not that easy, as a BFILE can't just be constructed. Here's an Oracle tutorial for working with BFILES in Java.


CommonsMultipartFile represents multipart form request, i.e. a collection of fields. You have to extract your file from the field, i.e. call getFileItem() or getInputStream() to extract the file content.

0

精彩评论

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

关注公众号