简体   繁体   English

如何将java.sql.Blob写入JPA实体?

[英]How to write java.sql.Blob to JPA entity?

I have a JPA entity with java.sql.Blob: 我有一个带java.sql.Blob的JPA实体:

@Entity
public class LargeData {

  @Lob
  private java.sql.Blob data;

  //getters/setters
}

How to create instance of this entity? 如何创建这个实体的实例? I want to set Blob with setData() method, but how to get Blob from JPA? 我想用setData()方法设置Blob ,但是如何从JPA获取Blob java.sql.Blob is only interface, there are different implementations for different databases, so I assume JPA should give me right implementation. java.sql.Blob只是接口,不同的数据库有不同的实现,所以我假设JPA应该给我正确的实现。 How to get it? 怎么弄?

Use a byte array: 使用字节数组:

@Lob
@Column(length=100000)
private byte[] data;

If you want to use streams, create the blob using Hibernate.createBlob(..) 如果要使用流,请使用Hibernate.createBlob(..)创建blob

use a file stream. 使用文件流。 However, this appears to have various complications, depending on your database, driver, and JPA implementation. 但是,这似乎有各种复杂性,具体取决于您的数据库,驱动程序和JPA实现。 I built a generic solution, that was slow, and failed with large files, then found a Hibernate-specific solution that worked with Oracle 11.2.0.4 我构建了一个通用的解决方案,这个解决方案速度很慢,并且在使用大型文件时失败了,然后找到了一个适用于Oracle 11.2.0.4的特定于Hibernate的解决方案

I'm using Spring Data / JPA, but the issue seems to be around Hibernate, not Spring. 我正在使用Spring Data / JPA,但问题似乎是围绕Hibernate,而不是Spring。

Hibernate: 休眠:

private void testLoadFile() throws SQLException, IOException {


  File f = new File("//C:/tmp/6mb_file.wmv");
  BufferedInputStream fstream = new BufferedInputStream(new FileInputStream(f));

  Session session = entityManager.unwrap(Session.class);
  Blob blob = Hibernate.getLobCreator(session).createBlob(fstream, f.length());

  FileBLOBEntity file = new FileBLOBEntity();

  file.setName("//C:/tmp/6mb_file.wmv");
  file.setTheData(blob);
  blobRepository.saveAndFlush(file);
}

Generic Spring/JPA: Generic Spring / JPA:

private void testLoadFile() throws SQLException, IOException {

  File f = new File("//C:/tmp/6mb_file.wmv");
  BufferedInputStream fstream = new BufferedInputStream(new FileInputStream(f));

  Blob blob = connection.getConnection().createBlob();
  BufferedOutputStream bstream = new  BufferedOutputStream(blob.setBinaryStream(1));
  // stream copy runs a high-speed upload across the network
  StreamUtils.copy(fstream, bstream);

  FileBLOBEntity file = new FileBLOBEntity();

  file.setName("//C:/tmp/6mb_file.wmv");
  file.setTheData(blob);
  // save runs a low-speed download across the network.  this is where
  // Spring does the SQL insert.  For a large file, I get an OutOfMemory exception here.
  blobRepository.saveAndFlush(file);
}

and for retrieval: 并用于检索:

public void unloadFile() throws SQLException, IOException {

  File f = new File("//C:/tmp/6mb_file.wmv" + "_fromDb");

  FileOutputStream fstream = new FileOutputStream(f);

  FileBLOBEntity file = blobRepository.findByName("//C:/tmp/6mb_file.wmv");
  Blob data = file.getTheData();

  InputStream bstream = data.getBinaryStream();
  StreamUtils.copy(bstream, fstream);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM