I have a JPA entity with java.sql.Blob:
@Entity
public class LargeData {
@Lob
private java.sql.Blob data;
//getters/setters
}
How to create instance of this entity? I want to set Blob
with setData()
method, but how to get Blob
from JPA? java.sql.Blob
is only interface, there are different implementations for different databases, so I assume JPA should give me right implementation. How to get it?
Use a byte array:
If you want to use streams, create the blob using
Hibernate.createBlob(..)
use a file stream. However, this appears to have various complications, depending on your database, driver, and JPA implementation. I built a generic solution, that was slow, and failed with large files, then found a Hibernate-specific solution that worked with Oracle 11.2.0.4
I'm using Spring Data / JPA, but the issue seems to be around Hibernate, not Spring.
Hibernate:
Generic Spring/JPA:
and for retrieval: