【问题标题】:How to make field be BLOB not TINYBLOB?如何使字段成为 BLOB 而不是 TINYBLOB?
【发布时间】:2012-09-18 10:21:44
【问题描述】:

如何使字段成为 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 的注释。

【问题讨论】:

    标签: java image hibernate orm blob


    【解决方案1】:

    你可以这样试试;

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

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

    【讨论】:

      猜你喜欢
      • 2014-06-26
      • 1970-01-01
      • 2019-04-18
      • 2014-07-06
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多