Java 使用springdata将存储为blob的图像直接读取到字节[]中

Java 使用springdata将存储为blob的图像直接读取到字节[]中,java,hibernate,jpa,blob,spring-data,Java,Hibernate,Jpa,Blob,Spring Data,我有一个存储在Oracle数据库中的图像,我正在使用spring数据检索该图像 @Query("SELECT c.binaryContent from ContentEntity c join c.ParentContentEntities pce where pce.SpecificEntity.id = :id and pce.contentType.id = 11") byte [] getImageBinaryContent(@Param("id") Long id); @Lob @

我有一个存储在Oracle数据库中的图像,我正在使用spring数据检索该图像

@Query("SELECT c.binaryContent from ContentEntity c join c.ParentContentEntities pce where pce.SpecificEntity.id = :id and pce.contentType.id = 11")
byte [] getImageBinaryContent(@Param("id") Long id);


@Lob
@Column(name = "BINARY_CONTENT")
private byte [] binaryContent;


byte[] testImageArray = serviceLayer.getImageBinaryContent(id) RETURNS NULL
单独测试此查询是否有效。它查找内容,但当调用传递id的getImageBinaryContent时,我什么也没有得到,只有null结果。如果我将返回类型更改为blob,我将成功地返回blob


为什么我不能直接将blob读入字节数组?我的搜索显示了返回blob,然后将带有inputstream的blob转换为字节数组的示例,但似乎我应该可以直接执行此操作。

您不能仅强制转换为不兼容的类型。一个
byte[]
不同于一个
Blob
。如果有办法做到这一点就好了,因为对于spring数据,所有查询都是在接口中完成的。如果要返回字节数组或流,则必须为每个包含二进制数据的repo创建自定义存储库。在我看来,从您的数据/存储库层返回一个BLOB有点范围蠕变。是否有可注册的“转换器”或可用于处理此问题的东西?定制实现钩住实用程序转换方法的替代方案并不简单