Quote:
OK.. but getting data *into* the blob is the challenge.. Are you saying I don't need a custom type, just call Hibernate.createBlob() with whatever I stream the information into? I found that to be problematic..
yes
I have mapping like one in my post and read and save image like this :
Code:
public void testRead() throws IOException, SQLException {
String id = "2";
//OrgZap orgZap = (OrgZap) getSession().get(OrgZap.class, id);
OrgZapImage orgZapImage = (OrgZapImage) getSession().get(
OrgZapImage.class, id);
InputStream input = getClass().getResourceAsStream(
"/images/maradona_thumb.gif");
byte bytes[] = new byte[input.available()];
int i = input.read(bytes);
Blob readBlob = orgZapImage.getImage();
int len = (int) readBlob.length();
byte[] readBytes = readBlob.getBytes(1, len);
assertTrue("blob and read blob are same",bytes.length == readBytes.length);
for (int j=0;j<bytes.length;j++)
assertTrue("test byte " + j,bytes[j] == readBytes[j]);
}
public void testSave() throws IOException, SQLException {
String id = "2";
OrgZap orgZap = (OrgZap) getSession().get(OrgZap.class, id);
OrgZapImage orgZapImage = (OrgZapImage) getSession().get(
OrgZapImage.class, id);
if (orgZapImage != null)
getSession().delete(orgZapImage);
orgZapImage = new OrgZapImage();
InputStream input = getClass().getResourceAsStream(
"/images/maradona_thumb.gif");
byte bytes[] = new byte[input.available()];
int i = input.read(bytes);
Blob blob = Hibernate.createBlob(bytes);
orgZapImage.setId(orgZap.getId());
orgZapImage.setImage(blob);
Transaction t = getSession().beginTransaction();
getSession().save(orgZapImage);
t.commit();
getSession().evict(orgZapImage);
OrgZapImage orgZapImage1 = (OrgZapImage) getSession().get(
OrgZapImage.class, orgZap.getId());
Blob readBlob = orgZapImage1.getImage();
int len = (int) readBlob.length();
byte[] readBytes = readBlob.getBytes(1, len);
assertTrue("blob and read blob are same",bytes.length == readBytes.length);
}