Am1*_*3zA 4 java hibernate blob image
我想知道在hibernate的帮助下存储图像的最佳方法是什么(进入MySQL)我有这个类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;
Run Code Online (Sandbox Code Playgroud)
另外,我有PictureDAO; 我想知道如何实现My PictureDAO来保存和检索图像.
带字节数组的版本很简单.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8516 次 |
| 最近记录: |