Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将java.sql.Blob写入JPA实体?_Java_Jpa_Blob - Fatal编程技术网

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

如何将java.sql.Blob写入JPA实体?,java,jpa,blob,Java,Jpa,Blob,我有一个带有java.sql.Blob的JPA实体: @Entity public class LargeData { @Lob private java.sql.Blob data; //getters/setters } 如何创建此实体的实例?我想用setData()方法设置Blob,但是如何从JPA获取Blobjava.sql.Blob是唯一的接口,不同的数据库有不同的实现,所以我认为JPA应该给我正确的实现。如何获取它?使用字节数组: @Lob @Column(leng

我有一个带有java.sql.Blob的JPA实体:

@Entity
public class LargeData {

  @Lob
  private java.sql.Blob data;

  //getters/setters
}
如何创建此实体的实例?我想用
setData()
方法设置
Blob
,但是如何从JPA获取
Blob
java.sql.Blob
是唯一的接口,不同的数据库有不同的实现,所以我认为JPA应该给我正确的实现。如何获取它?

使用字节数组:

@Lob
@Column(length=100000)
private byte[] data;
如果要使用流,请使用文件流创建blob。 但是,这似乎有各种复杂情况,具体取决于您的数据库、驱动程序和JPA实现。 我构建了一个通用的解决方案,速度很慢,但在处理大文件时失败,然后找到了一个与Oracle 11.2.0.4配合使用的特定于Hibernate的解决方案

我使用的是SpringData/JPA,但问题似乎与Hibernate有关,而不是Spring

休眠:

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);
}
通用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);
}
以及检索:

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

}

接受答案的附加信息。2016年个人经验证实。tl;dr:>length属性用于定义字符串字段的>列长度(对于其他类型,它被忽略)。这对我来说使用clob,而不是blob。我不指定
length
属性。我还使用java nio从文件中读取:
myEntity.data=Files.readAllBytes(myPath)
。可以工作,但特定于Hibernate,如果使用EclipseLink,它将中断。。。这是我在帖子顶部说的。我不喜欢特定于实现,但这是我发现的唯一既稳定又足够快的解决方案。