Java 使用 Hibernate 以块的形式读取/写入 blob 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20614973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:08:37  来源:igfitidea点击:

Read/write blob data in chunks with Hibernate

javaoraclehibernatestreamblob

提问by Atticus

Is there a way to read and write from a blobin chunks using Hibernate. Right now I am getting OutOfmemoryExceptionbecause the whole blob data is loaded in memory into a byte[].

有没有办法blob使用Hibernate. 现在我得到了,OutOfmemoryException因为整个 blob 数据都加载到内存中的byte[].

To be more specific, let's say I want to save a large file into a database table called File.

更具体地说,假设我想将一个大文件保存到名为File.

public class File {
   private byte[] data;
}

I open the file in a FileInputStream and then what? How do I tell Hibernate that I need to stream the content and will not give the whole byte[]array at once? Should I use Blobinstead of byte[]? Anyway how can I stream the content?

我在 FileInputStream 中打开文件,然后呢?我如何告诉 Hibernate 我需要流式传输内容并且不会立即提供整个byte[]数组?我应该使用Blob而不是byte[]吗?无论如何,我如何流式传输内容?

Regarding reading, is there a way I can tell hibernate that (besides the lazy loading it does) I need the blob to be loaded in chunks, so when I retrieve my Fileit should not give me OutOfMemoryException.

关于阅读,有没有一种方法可以告诉 hibernate(除了它所做的延迟加载之外)我需要将 blob 分块加载,因此当我检索File它时,它不应该给我OutOfMemoryException.

I am using:

我在用:

  • Oracle 11.2.0.3.0
  • Hibernate 4.2.3 Final
  • Oracle Driver 11.2
  • 甲骨文 11.2.0.3.0
  • 休眠 4.2.3 最终版
  • Oracle 驱动程序 11.2

采纳答案by Roy Six

If going the Blob route, have you tried using Hibernate's LobHelpercreateBlobmethod, which takes an InputStream? To create a Blob and persist to the database, you would supply the FileInputStream object and the number of bytes.

如果走 Blob 路线,您是否尝试过使用 Hibernate 的LobHelpercreateBlob方法,该方法采用InputStream? 要创建 Blob 并持久保存到数据库,您需要提供 FileInputStream 对象和字节数。

Your File bean/entity class could map the Blob like this (using JPA annotations):

您的 File bean/实体类可以像这样映射 Blob(使用 JPA 注释):

@Lob
@Column(name = "DATA")
private Blob data;

// Getter and setter

And the business logic/data access class could create the Blob for your bean/entity object like this, taking care not to close the input stream before persisting to the database:

并且业务逻辑/数据访问类可以像这样为您的 bean/实体对象创建 Blob,注意不要在持久化到数据库之前关闭输入流:

FileInputStream fis = new FileInputStream(file);
Blob data = getSession().getLobHelper().createBlob(fis, file.length());
fileEntity.setData(data);
// Persist file entity object to database

To go the other way and read the Blob from the database as a stream in chunks, you could call the Blob's getBinaryStreammethod, giving you the InputStream and allowing you to set the buffer size later if needed:

反过来,从数据库中读取 Blob 作为块中的流,您可以调用 Blob 的getBinaryStream方法,为您提供 InputStream 并允许您稍后根据需要设置缓冲区大小:

InputStream is = fileEntity.getData().getBinaryStream();

Struts 2 has a convenient configuration available that can set the InputStream result's buffer size.

Struts 2有一个方便的配置,可以设置 InputStream 结果的缓冲区大小