3

如何使字段成为 BLOB 而不是 TINYBLOB?

我的映射如下:

private byte[] ImageBytes;

public BufferedImage getImage() {
    InputStream in = new ByteArrayInputStream(ImageBytes);
    try {
        return ImageIO.read(in);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

public void setImage(BufferedImage image) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        ImageIO.write(image, "PNG" /* for instance */, out);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    ImageBytes = out.toByteArray();
}

它会导致 Hibernate 在 MySQL 中创建不适合图像的 TINYBLOB 字段。

如何让它使用 BLOB?

下面的回答

@Lob(type = LobType.BLOB)

不起作用,因为@Lob注释在我的库中没有参数。

此外,我不想使用特定于 ORM 或特定于 DBMS 的注释。

4

1 回答 1

2

你可以这样试试;

@Lob
@Column(name="IMAGE", nullable=false, columnDefinition="blob")
private byte[] imageBytes;

并确保您的图像数据类型是BLOB.

于 2012-09-18T11:21:05.097 回答