简体   繁体   English

无法使用JPA将java.sql.BLOB持久化到Oracle BLOB中

[英]Unable to persist java.sql.BLOB into oracle BLOB using JPA

I am trying to persist the image as a blob in oracle database(11g) using JPA(2.0) 我正在尝试使用JPA(2.0)将图像作为blob保留在oracle数据库(11g)中

Below is my entity class 以下是我的实体课

@Entity
@Table(name="MyTable")
public class MyEntity implements Serializable {

@Column(name="IMAGE_BLOB")
private  Blob imageBlob;

public void setImageBlob(Blob imageBlob) {
this.imageBlob = imageBlob;
}

public Blob getImageBlob() {
return imageBlob;
}

} }

Below is my code where am trying to set the blob 下面是我的代码试图设置斑点

InputSteram fis = new FileInputStream("C://folder1/folder2/image1.jpg");
byte[] imageByteArray= IOUtils.toByteArray(fis);

Blob imageBlob = new SerialBlob(imageByteArray);

MyEntity myEntity = new MyEntity();
myEntity.setImageBlob(imageBlob ).

when i run the above code snippet and persist MyEntity into the database, I see all the values get properly persisted in the database except the IMAGE_BLOB column value which shows null, It dint throw any exception. 当我运行上面的代码片段并将MyEntity持久保存到数据库中时,我看到所有值都正确保存在数据库中,除了IMAGE_BLOB列值显示为null之外,它不会抛出任何异常。 I also verified that the imageBlob am trying to set is not null. 我还验证了imageBlob试图设置的值不为null。

Could any one please help me with this issue. 谁能帮我解决这个问题。

@LOB  
@Column(name="IMAGE_BLOB")  
private  byte[] imageBlob;

It works well and I done it in Oracle DB also. 它运作良好,我也在Oracle DB中做到了。 So if you get the ClassCastException then there will be a problem in your Java code. 因此,如果您收到ClassCastException那么您的Java代码中将出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM