Hi,
as per the above mentioned post, I've tried making use of BFile user type.
Code:
POJO making use of BFileType: TestBfile.java
import java.io.Serializable;
@SuppressWarnings("serial")
public class TestBfile implements Serializable {
private String evtDocId;
private String fileName;
private byte[] filePath;
private long version;
/**
* @return the evtDocId
*/
public String getEvtDocId() {
return evtDocId;
}
/**
* @param evtDocId the evtDocId to set
*/
public void setEvtDocId(String evtDocId) {
this.evtDocId = evtDocId;
}
/**
* @return the fileName
*/
public String getFileName() {
return fileName;
}
/**
* @param fileName the fileName to set
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* @return the filePath
*/
public byte[] getFilePath() {
return filePath;
}
/**
* @param filePath the filePath to set
*/
public void setFilePath(byte[] filePath) {
this.filePath = filePath;
}
/**
* @return the version
*/
public long getVersion() {
return version;
}
/**
* @param version the version to set
*/
public void setVersion(long version) {
this.version = version;
}
}
mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 27, 2008 11:49:03 AM by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="com.gpj.gl.pa.domain.TestBfile" table="Test_DOCUMENTS_TX" schema="GL_PA">
<id name="evtDocId" type="string">
<column name="EVT_DOC_ID" length="15" />
<generator class="sequence">
<param name="sequence">
PAParticipation
</param>
</generator>
</id>
<version name="version" type="long">
<column name="VERSION" precision="15" scale="0" not-null="true" />
</version>
<property name="fileName" type="string">
<column name="FILE_NAME" length="50" not-null="true" />
</property>
<property name="filePath" type="com.util.BFileType">
<column name="FILE_PATH" not-null="true" />
</property>
</class>
</hibernate-mapping>
BFileType class:
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import oracle.jdbc.driver.OracleConnection;
import oracle.jdbc.driver.OracleTypes;
import oracle.sql.BFILE;
@SuppressWarnings("unchecked")
public final class BFileType implements UserType {
private static final int BFILE_TYPE = OracleTypes.BFILE;
private static final Class RETURNED_CLASS = byte[].class;;
public int[] sqlTypes() {
return new int[]{BFILE_TYPE};
}
public Class returnedClass() {
return RETURNED_CLASS;
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y){
return true;
}
if (x == null || y == null){
return false;
}
return x.equals(y);
}
public boolean isMutable() {
return false;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
BFILE file = (BFILE) rs.getObject(names[0]);
if (file == null)
return null;
return file.getBytes();
}
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
if (value == null)
st.setObject(index,null);
else {
BFILE file = new BFILE((OracleConnection) st.getConnection(),(byte[])value);
st.setObject(index, file); // getting exception here.
}
}
public Object assemble(Serializable arg0, Object arg1)
throws HibernateException {
// TODO Auto-generated method stub
return null;
}
public Object deepCopy(Object arg0) throws HibernateException {
// TODO Auto-generated method stub
return arg0;
}
public Serializable disassemble(Object arg0) throws HibernateException {
// TODO Auto-generated method stub
return null;
}
public int hashCode(Object arg0) throws HibernateException {
// TODO Auto-generated method stub
return arg0.hashCode();
}
public Object replace(Object arg0, Object arg1, Object arg2)
throws HibernateException {
// TODO Auto-generated method stub
return null;
}
}
I am getting an Exception, in
BFileType class at line
st.setObject(index, file);
Exception is:
Hibernate: insert into GL_PA.Test_DOCUMENTS_TX (VERSION, FILE_NAME, FILE_PATH, EVT_DOC_ID) values (?, ?, ?, ?)
2008-07-07 18:21:38,955 ERROR [org.hibernate.jdbc.AbstractBatcher] - <Exception executing batch: >
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at oracle.jdbc.driver.DatumBinder.bind(OraclePreparedStatement.java:15862)
at oracle.jdbc.driver.OraclePreparedStatement.setupBindBuffers(OraclePreparedStatement.java:2866)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10580)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.gpj.gl.pa.test.BFileTest.main(BFileTest.java:39)
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at oracle.jdbc.driver.DatumBinder.bind(OraclePreparedStatement.java:15862)
at oracle.jdbc.driver.OraclePreparedStatement.setupBindBuffers(OraclePreparedStatement.java:2866)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10580)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.gpj.gl.pa.test.BFileTest.main(BFileTest.java:39)
Please provide your suggestions why I am getting this exception.