oracle - Springboot 2.4 ORA-00932:不一致的数据类型:预期 - 得到 BLOB
问题描述
我正在尝试将映像保存到 Oracle 数据库(Oracle 数据库 12c 企业版版本 12.1.0.2.0)。每次我使用我的存储库保存它(更新或插入语句)时,我都会收到以下异常:
oracle.jdbc.OracleDatabaseException: ORA-00932: inconsistent datatypes expected - got BLOB
我的实体
@Entity
public class Profile {
@Id
private Long id;
@Column
private String name;
@Lob
@Column
private byte[] image;
//getters and setters
}
我的仓库:
@Repository
public interface ProfileRepository extends JpaRepository<Profile, Long> {
}
最后但并非最不重要的是我的服务方法:
@Transactional
public void saveProfile(Profile profile) {
profileRepository.save(profile);
}
我的 ddl 脚本:
create table "profile"
(
image BLOB not null,
name varchar2(30) not null,
ID NUMBER no null primary key
);
我试图将图像字段类型更改为,java.sql.Blob
但它也不起作用。有谁知道如何解决这个问题?
解决方案
尝试image
用
@Column
@Blob
private Blob image;
当你想显示它时,转换它:
private byte[] toByteArray(Blob fromImageBlob) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
return toByteArrayImpl(fromImageBlob, baos);
} catch (Exception e) {
}
return null;
}
private byte[] toByteArrayImpl(Blob fromImageBlob,
ByteArrayOutputStream baos) throws SQLException, IOException {
byte buf[] = new byte[4000];
int dataSize;
InputStream is = fromImageBlob.getBinaryStream();
try {
while((dataSize = is.read(buf)) != -1) {
baos.write(buf, 0, dataSize);
}
} finally {
if(is != null) {
is.close();
}
}
return baos.toByteArray();
}
推荐阅读
- php - Content-Length 标头正确,但当 headers+body = content-length 时下载工具停止
- flutter - 飞镖冻结转换为另一种数据类型
- php - 固定多对一的 symfony
- python - discord.py 错误,同时给一个人一个静音的角色
- java - AWS-ECS 部署得到 404 NOT FOUND
- c++ - IBUS-警告**:19:51:34.312:创建输入上下文失败:达到超时wxWidgets
- reactjs - 检测设备的输入键 IonSearchBar
- javascript - 为什么反应组件内部的功能不起作用?
- postgresql - 无法在 WSL Arch 上启动 postgresql
- css - 无法通过出现错误“AttributeError:“str”对象没有属性“clip”来调整胸部 X 光图像的大小