Difficulty setting ArrayList to java.sql.Blob to save in DB using hibernate

Posted by me_here on Stack Overflow See other posts from Stack Overflow or by me_here
Published on 2010-06-10T11:43:08Z Indexed on 2010/06/10 11:52 UTC
Read the original article Hit count: 422

Filed under:
|
|

I'm trying to save a java ArrayList in a database (H2) by setting it as a blob, for retrieval later. If this is a bad approach, please say - I haven't been able to find much information on this area.

I have a column of type Blob in the database, and Hibernate maps to this with java.sql.Blob. The code I'm struggling with is:

Drawings drawing = new Drawings();

try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    oos = new ObjectOutputStream(bos);
    oos.writeObject(plan.drawingPane21.pointList);
    byte[] buff = bos.toByteArray();
    Blob drawingBlob = null;
    drawingBlob.setBytes(0, buff);
    drawing.setDrawingObject(drawingBlob);
} catch (Exception e){
    System.err.println(e);
}

The object I'm trying to save into a blob (plan.drawingPane21.pointList) is of type ArrayList<DrawingDot>, DrawingDot being a custom class implementing Serializable.

My code is failing on the line drawingBlob.setBytes(0, buff); with a NullPointerException.

Help appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate