我正在屏幕上工作,它将以BFILE类型将文件上传到oracle表。我正在使用spring3和hibernate3。

BO类如下所示:

@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;
}

}


我定义了一个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 isMutable() { 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);
}


}

问题是当我尝试在控制器中设置setFile时

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

它编译良好,但是当我从屏幕上传文件时,出现以下异常

java.lang.ClassCastException:org.springframework.web.multipart.commons.CommonsMultipartFile无法转换为oracle.sql.BFILE

如何解决呢?

解决了 :

我试过了 :

public void nullSafeSet(PreparedStatement st,Object value,int index)引发HibernateException,SQLException {
        if(value == null){
            st.setObject(index,null);
        }其他{

        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);
    }
}


及其工作正常。

谢谢!

最佳答案

java.lang.ClassCastException:
  org.springframework.web.multipart.commons.CommonsMultipartFile无法
  强制转换为oracle.sql.BFILE


是正确的,这两个类没有共享类型层次结构,无法将一个类型转换为另一个类型。而不是强制转换类型,您应该专注于传输内容。

这应该是您需要的代码:

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


更新:事实并非如此,因为不能仅构造BFILE。这是Oracle tutorial for working with BFILES in Java

10-08 06:56