JPA - analog of LobCreator from hibernate?
hibernate, jakarta-ee, java, jpa
Solution
You can use the `@Lob` annotation on your persistent property (Annotation Lob):
@Entity
public class MyEntity {
private byte[] content;
...
@Lob
public byte[] getContent() {
return content;
}
public void setContent(byte[] newContent) {
this.content = newContent;
}
}
In your code you can transform a stream in a byte[] with a code like this:
@Transient
public void setContentFromInputStream(InputStream is) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int l = 0;
do {
l = is.read(buff);
if (l > 0)
{
baos.write(buff, 0, l);
}
} while (l > 0);
is.close();
baos.flush();
baos.close();
content = baos.toByteArray();
}
The `@Lob` annotation can also be used with String, in this case you'll obtain a CLOB on the DB
You must pay attention to the size of the `byte[]` to avoid `OutOfMemoryError`.
To use only streams you must rely on the specific jdbc vendor implementation. For example if you are using Hibernate >= 3.6 you can change they type of MyEntity.content to Blob and write:
MyEntity entity = new MyEntity();
Session session = (Session)entityManager.getDelegate();
Blob newContent = session.getLobHelper().createBlob(inputStream, len);
entity.setContent(newContent);
I hope this can help you
Problem
in pure Hibernate i can do: ``` Blob blob= Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(inputStream, len); ``` How to do this in jpa (with hibernate as provider)? In pure hibernate i made user type for blobs, which used `setBinaryStream` prepared statement. This solution worked for me perfectly and i'm looking for a way to port it to JPA.