Here's a mapping:
Code:
<hibernate-mapping default-cascade="all">
<class name="Graphic" table="graphics">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="data" type="binary" column="data" not-null="true" unique="false"/>
</class>
</hibernate-mapping>
The class:
Code:
import java.io.Serializable;
import net.sf.hibernate.Session;
public class Graphic implements Serializable {
private byte[] data;
public Graphic() {}
public GraphicAsset(byte[] data) {
this.data = data;
}
public byte[] getData() {
return this.data;
}
public void setData(byte[] data) {
this.data = data;
}
}
Loading one up:
Code:
import java.io.*;
public class GraphicLoader {
public static void main(String[] args) throws IOException {
Session session = ...get your hibernate session...
Graphic graphic = new Graphic(readImage(args[0]));
session.save(graphic);
session.close();
}
private static final byte[] readImage(String imageName)
throws IOException {
FileInputStream fis =
new FileInputStream(imageName);
byte[] buf = new byte[4096];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
while((len = fis.read(buf)) != -1) {
baos.write(
buf,
0,
len
);
}
return baos.toByteArray();
}
}