首页 > 解决方案 > 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但它也不起作用。有谁知道如何解决这个问题?

标签: oraclespring-boothibernatejpaspring-data-jpa

解决方案


尝试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();
  }

推荐阅读