使用Hibernate在Blob中存储Blob

时间:2014-07-24 06:16:30

标签: java hibernate oracle10g

我需要你的帮助,使用hibernate将Blob存储到Oracle 10g数据库中。 我正在尝试,并尝试将Blob存储在DB(Oracle 10g)中。

我为此制作了一个简单的程序,它使用java.sql.Blob类型来存储在DB中。 我运行TestBlobStore程序时遇到的错误是:

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)

我的豆是:

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

我的TestBlobInsertDemo文件是:

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

}

我的映射文件是这样的:

<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>

我知道,如何使用Bytearray存储Blob并成功学习它,我想知道如何使用java.sql.Blob。

请帮我解释一下。 我已经google了几乎所有使用bytearray。 我采用存储java.sql.Blob的方法是为了简化我的代码。

感谢您帮助我。

0 个答案:

没有答案