Hibernate 4.2.2从未知长度的输入流创建blob [英] Hibernate 4.2.2 create blob from unknown-length input stream

查看:124
本文介绍了Hibernate 4.2.2从未知长度的输入流创建blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想从输入流创建一个hibernate blob,但我不知道流的长度。

  Hibernate.getLobCreator(sessionFactory.getCurrentSession())。createBlob(stream,length)

我在不知道流的长度的情况下创建了一个blob?

EDIT1



在更旧的hibernate版本中,

http://viralpatel.net/blogs/tutorial-save-get-blob-object-spring-3-mvc-hibernate/



Blob blob = Hibernate.createBlob(file.getInputStream());

EDIT2



好的但它有一个错误的实现



返回新的SerializableBlob(新的BlobImpl(stream,stream.available()));

stream.available不是真正的大小



编辑3



我试过了

  session.doWork(new Work(){
@Override
public void execute(Connection conn)throws SQLException {
LargeObjectManager lobj =((org.postgresql.PGConnection)conn ).getLargeObjectAPI();

但conn只是c3p0的一个NewProxyConnection。

解决方案

以下是我现在使用的内容

 会话currentSession = getSessionFactory()。getCurrentSession(); 
Blob blob = Hibernate.getLobCreator(currentSession).createBlob(new byte [0]);
OutputStream setBinaryStream = blob.setBinaryStream(1);
Utils.fastChannelCopy(input,setBinaryStream);
setBinaryStream.close();


Hi i want to create a blob in hibernate from an inputstream, but i don't know the length of the stream.

Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(stream, length)

how can i crate a blob without knowing the length of the stream?

EDIT1

in older hibernate versions it was possible

http://viralpatel.net/blogs/tutorial-save-get-blob-object-spring-3-mvc-hibernate/

Blob blob = Hibernate.createBlob(file.getInputStream());

EDIT2

ok but it had an buggy implementation

return new SerializableBlob( new BlobImpl( stream, stream.available() ) );

stream.available isn't the real size

EDIT 3

i tried

session.doWork(new Work() {
            @Override
            public void execute(Connection conn) throws SQLException {
            LargeObjectManager lobj = ((org.postgresql.PGConnection) conn).getLargeObjectAPI();

but conn is just a NewProxyConnection from c3p0.

解决方案

Here is what i'm using now

Session currentSession = getSessionFactory().getCurrentSession();
Blob blob = Hibernate.getLobCreator(currentSession).createBlob(new byte[0]);
OutputStream setBinaryStream = blob.setBinaryStream(1);
Utils.fastChannelCopy(input, setBinaryStream);
setBinaryStream.close();

这篇关于Hibernate 4.2.2从未知长度的输入流创建blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆