views:

1226

answers:

1

Hi every One, can any one tell me what is the best way to store and Image By hibernate (into MySQL) I have this class Mapping

@Entity
@Table(name = "picture")
public class PictureEntity implements Serializable {

    @Id
    @Column(name = "id")
    @GeneratedValue
    private int id;
    @Column(name = "format", length = 8)
    private String format;
    //@Basic(fetch = FetchType.LAZY)
    @Lob
    @Column(name = "context", nullable = true, columnDefinition = "mediumblob")
    private java.sql.Blob myBlobAttribute; // or byte[] no diff
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "branch_fk", referencedColumnName = "id", nullable = false)
    private BranchEntity branch;

and also have PictureDAO I wanna know How shoud I implement My PictureDAO to save and retrieve Image.

+1  A: 

The version with the byte array is simple.

public class PictureEntity implements Serializable {
    private byte[] imageBytes;

    public BufferedImage getImage() {
        InputStream in = new ByteArrayInputStream(imageBytes);
        return ImageIO.read(in);
    }

    public void setImage(BufferedImage image) {
        BufferedOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image, "PNG" /* for instance */, out);
        imageBytes = out.toByteArray();
    }
}
Maurice Perry
? you forget to complete or you want write too short?
Am1rr3zA
Sorry about that, I submitted the form by error.
Maurice Perry
Tanx for you Edit but it's don't help me I dun want setter and getter I want PictureDAo Implementation
Am1rr3zA