1

I need your help in storing a Blob into Oracle 10g DB , using hibernate. I was experimenting , and trying to store Blob in DB (Oracle 10g).

For which I made a simple Program, which uses java.sql.Blob type to get stored in DB. The error I am getting when I run the TestBlobStore program is :

Hibernate: insert into SIMPLE_BEANS (DATA, SIMPLE_BEAN_ID) values (?, ?)
Exception in thread "main" java.lang.AbstractMethodError: oracle.jdbc.driver.T4CPreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V
    at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$5$1.doBind(BlobTypeDescriptor.java:151)
    at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$2$1.doBind(BlobTypeDescriptor.java:107)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:90)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
    at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3121)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at blobStore.test.TestBlobStore.main(TestBlobStore.java:45)

My Bean is :

SimpleBean
{
private Long id;
private Blob data;
//getter and Setters
}

My TestBlobInsertDemo file is:

public class TestBlobStore {

    public static void main(String[] args) throws IOException {

        Configuration cfg = new Configuration();
        cfg.configure();
        ServiceRegistry serRegObj = new StandardServiceRegistryBuilder()
                .applySettings(cfg.getProperties()).build();
        SessionFactory sessionFactory = cfg.buildSessionFactory(serRegObj);
        Session session = sessionFactory.openSession();

        session.beginTransaction();

        File dataFile = new File("xmlFile/test.xml");
        long dataSize = dataFile.length();
        InputStream dataStream = new FileInputStream(dataFile);

        LobHelper lobHelper = session.getLobHelper();
        Blob dataBlob = lobHelper.createBlob(dataStream, dataSize);

        SimpleBean myBean = new SimpleBean();
        myBean.setId(1L);
        myBean.setData(dataBlob);

        session.save(myBean);
        System.out.println("bean inserted");
        session.getTransaction().commit(); // Throws java.lang.OutOfMemoryError
        session.close();
        System.out.println("File Saved");
        // blobStream.close();
        sessionFactory.close();
    }

}

My mapping file is as such :

<hibernate-mapping>
<class name="blobStore.bean.SimpleBean" table="SIMPLE_BEANS">
    <id name="id" type="int" column="SIMPLE_BEAN_ID" />
    <property name="data" type="blob" column="DATA" />
</class>
</hibernate-mapping>

I know , how to use Bytearray to store Blob and have successfully learned it , I want to know how to use java.sql.Blob instead.

Please help me out to learn this. I have googled for this almost all have use bytearray. My intention of adopting approach of storing java.sql.Blob is to simplify my code.

Thank you for helping me out.

user3769778
  • 967
  • 2
  • 7
  • 26
  • 1
    With JDBC, that error usually occurs because your JDBC driver implements an older version of the JDBC API than the one included in your JRE. These older versions are fine so long as you don't try and use a method that appeared in the newer API. [This](http://stackoverflow.com/questions/2876956/help-in-setting-client-info-in-jdbc-for-oracle) might help you. – Ankur Singhal Jul 24 '14 at 08:07
  • Have the same problem, it's driver exception. Try another version. – Fireworks Jul 24 '14 at 12:07
  • I am using OJDBC14.jar , its the latest one. – user3769778 Jul 31 '14 at 06:45

0 Answers0