Deleting OID type BLOB for PostgreSQL

Specifically deleting a row that contains a Large Object reference does not delete the Large Object. Deleting the Large Object is a separate operation that needs to be performed..

In JDBC this can be done in two steps:

Delete the large object (call PostgreSQL function lo_unlink)

long oid = jdbcTemplate.queryForObject("select image from InventoryItem where id = ?",
			new Object[] { id },
			new RowMapper<Long>() {
			    public Long mapRow(ResultSet rs, int row) throws SQLException {
				return new Long(rs.getLong(1));
jdbcTemplate.execute("select lo_unlink(" + oid + ")");

Delete the row in the referring table:

jdbcTemplate.update("delete from InventoryItem where id = '" + id + "'");