【问题标题】:Springboot 2.4 ORA-00932: inconsistent datatypes: expected - got BLOBSpringboot 2.4 ORA-00932:不一致的数据类型:预期 - 得到 BLOB
【发布时间】:2021-06-18 11:03:54
【问题描述】:

我正在尝试将映像保存到 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,但它也不起作用。有谁知道如何解决这个问题?

【问题讨论】:

  • 您是如何尝试使用Blob 的,您遇到了什么错误?您必须使用@Lob 对其进行注释。你也可以分享堆栈跟踪吗?

标签: oracle spring-boot hibernate jpa spring-data-jpa


【解决方案1】:

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

【讨论】:

  • 如前所述,我已经尝试过,但没有用。
猜你喜欢
  • 1970-01-01
  • 2018-10-30
  • 2011-06-03
  • 1970-01-01
  • 2012-08-14
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多