Hibernate 4.2.2从未知长度的输入流创建blob

wut*_*aer 7 java hibernate blob stream

嗨,我想从输入流创建一个blob in hibernate,但我不知道流的长度.

Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(stream, length)
Run Code Online (Sandbox Code Playgroud)

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

EDIT1

在较旧的hibernate版本中,它是可能的

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

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

EDIT2

好吧,但它有一个错误的实施

返回新的SerializableBlob(new 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();
Run Code Online (Sandbox Code Playgroud)

但是conn只是来自c3p0的NewProxyConnection.

wut*_*aer 4

这是我现在使用的

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