@Lob
永続プロパティ ( Annotation Lob )で注釈を使用できます。
@Entity
public class MyEntity {
private byte[] content;
...
@Lob
public byte[] getContent() {
return content;
}
public void setContent(byte[] newContent) {
this.content = newContent;
}
}
あなたのコードでは、次のようなコードでストリームを byte[] に変換できます:
@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();
}
アノテーションは文字列でも使用できます。@Lob
この場合、DB で CLOB を取得します。
byte[]
回避するには、 のサイズに注意する必要がありOutOfMemoryError
ます。
ストリームのみを使用するには、特定の jdbc ベンダーの実装に依存する必要があります。たとえば、Hibernate >= 3.6 を使用している場合、MyEntity.content のタイプを Blob に変更して、次のように記述できます。
MyEntity entity = new MyEntity();
Session session = (Session)entityManager.getDelegate();
Blob newContent = session.getLobHelper().createBlob(inputStream, len);
entity.setContent(newContent);
これがお役に立てば幸いです