2009-07-24 19 views
4

我想知道什麼是圖像存儲與休眠(到MySQL)的幫助下,我有這個類的映射存儲和由Hibernate

@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; 

也是最好的辦法 ,我有PictureDAO檢索圖像;我想知道如何實現我的PictureDAO來保存和檢索圖像。

+0

設置/獲取myBlobAttribute值不會這樣做嗎?你有哪些錯誤?你有什麼嘗試? em.persist(entity),em.find(entity,id) – 2009-07-25 05:45:42

回答

5

帶字節數組的版本很簡單。

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) { 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     ImageIO.write(image, "PNG" /* for instance */, out); 
     imageBytes = out.toByteArray(); 
    } 
} 
+0

?你忘記完成了,或者你想寫得太短? – Am1rr3zA 2009-07-24 13:57:29