Quote:
I would probably rather use a property of blob type
Unfortunately blobs are a major pain in postgres. I'm using BYTEA type (byte[]) because of that.
Um...best practices? Dunno. The BYTEA type is limited in size, but I think the limit is in the gigabyte range. So con't store huge images. :) Other than that, I'm not sure what you're looking for.
Check here for an example I previously posted on reading the data:
http://forum.hibernate.org/viewtopic.php?t=927911
Here's a servlet doGet() method that outputs the same data, assuming it's a png:
Code:
public void doGet(
HttpServletRequest req,
HttpServletResponse resp
)
throws ServletException,
IOException {
Long id = new Long(req.getParameter("graphic_id"));
try {
Session session = <get your hibernate session here>
Graphic graphic = session.load(Graphic.class, id);
if(graphic == null) {
resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
} else {
resp.setContentType("image/png");
OutputStream out = resp.getOutputStream();
out.write(graphic.getData());
out.flush();
out.close();
}
} catch(HibernateException he) {
throw new ServletException("OOPS!", he);
}
}