java 使用 Hibernate 在 MySQL 中存储字节数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32572422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:24:52  来源:igfitidea点击:

Store byte array in MySQL with Hibernate

javamysqlspringhibernatejpa

提问by user2957378

I'm trying to save an entity with a byte array field. I'm using Hibernate and JPA on top of a MySQL database. This is the field definiton, which worked fine for an embedded H2 database:

我正在尝试使用字节数组字段保存实体。我在 MySQL 数据库之上使用 Hibernate 和 JPA。这是字段定义,它适用于嵌入式 H2 数据库:

@Entity(name = "blob")
public class Blob {
    ...
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "blobImg", nullable = false)
    private byte[] blobImg;
}

Now, with MySQL database, an exception is thrown everytime I execute blobRepository.save(). Actually, is may be thrown when Hibernate tries to autocreate the table of Blob entity. The exception is the following:

现在,对于 MySQL 数据库,每次执行 blobRepository.save() 时都会抛出异常。实际上,当 Hibernate 尝试自动创建 Blob 实体表时可能会抛出 is 。例外情况如下:

o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'blob (blobCols, blobImg, blobRows, channel, idBlobPersistence) values (50, _bina' at line 1

ohengine.jdbc.spi.SqlExceptionHelper :您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在“blob (blobCols、blobImg、blobRows、channel、idBlobPersistence)”值(第 1 行的 50、_bina)附近使用的正确语法

I've tried to change the field definition with several approaches I've found on the web:

我尝试使用我在网上找到的几种方法来更改字段定义:

Approach 1:

方法一:

@Column(name = "blobImg", nullable = false, columnDefinition = "BINARY(256)", length = 256)
private byte[] blobImg;

Approach 2:

方法二:

@Lob
@Column(name="blobImg", columnDefinition="bytea")
private byte[] blobImg;

Approach 3: Defining an hibernate mapping on blob.hbm.xml file and refering it from entityManagerFactory bean:

方法 3:在 blob.hbm.xml 文件上定义休眠映射并从 entityManagerFactory bean 中引用它:

<?xml version='1.0' encoding='UTF-8'?>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="mappingResources">  
        <list>  
            <value>blob.hbm.xml</value>  
        </list>  
    </property>
</bean>

blob.hbm.xml mapping:

blob.hbm.xml 映射:

<hibernate-mapping>
    <class name="guiatv.persistence.domain.Blob" table="blob">
        <property name="blobImgProperty">
            <column name="blobImg" sql-type="binary"></column>
        </property>
    </class>
</hibernate-mapping>

Approach 4: changing blob.hbm.xml mapping to the following:

方法 4:将 blob.hbm.xml 映射更改为以下内容:

<?xml version='1.0' encoding='UTF-8'?>
<hibernate-mapping>
    <class name="guiatv.persistence.domain.Blob" table="blob">
        <property name="blobImg" type="binary">
            <column name="blobImg" />
        </property>
    </class>
</hibernate-mapping>

All of them are throwing the same exception.

所有这些都抛出相同的异常。

How can I solve it? Thank you!

我该如何解决?谢谢!

采纳答案by user2957378

Well,

好,

I have tried your suggestions @Ernusc. The first one:

我已经尝试过你的建议@Ernusc。第一个:

@Lob(type = LobType.BLOB)
private byte[] blobImg;

it is no compiling for me. It may be my Hibernate version is different as yours. About the second option:

这对我来说不是编译。可能是我的 Hibernate 版本与您的不同。关于第二个选项:

@Type(type = "org.hibernate.type.BlobType")
@Lob
private byte[] blobImg;

It does compile, but it fails at runtime. It was showing the following error:

它可以编译,但在运行时失败。它显示以下错误:

2015-09-14 23:21:01.324  WARN 7436 --- [bTaskExecutor-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1064, SQLState: 42000
2015-09-14 23:21:01.324 ERROR 7436 --- [bTaskExecutor-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'blob blob0_ where blob0_.idBlobPersistence=326' at line 1
2015-09-14 23:21:01.337  INFO 7436 --- [bTaskExecutor-3] o.h.e.internal.DefaultLoadEventListener  : HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: could not extract ResultSet   

Also, when I tried to follow the trace, I found it was actually throwing the following exception:

此外,当我尝试跟踪跟踪时,我发现它实际上抛出了以下异常:

java.lang.ClassCastException: [B cannot be cast to java.sql.Blob

Then I realized the first of the error messages was actually caused by my stupid idea of naming my entity as "blob", which is probably a reserved keyword in Hibernate. So I changed the entity declaration from:

然后我意识到第一条错误消息实际上是由我将实体命名为“blob”的愚蠢想法引起的,这可能是 Hibernate 中的保留关键字。所以我改变了实体声明:

@Entity(name = "blob")
public class Blob {
   ...
}

to:

到:

@Entity(name = "blobframe")
public class Blob {
   ...
}

Then, your second suggestion was only throwing that ClassCastException exception. However, some of the approaches I tried before do work now. For instance:

然后,您的第二个建议只是抛出 ClassCastException 异常。但是,我之前尝试过的一些方法现在确实有效。例如:

@Column(name = "blobImg", nullable = false, columnDefinition = "BINARY(256)", length = 256)
private byte[] blobImg;

UPDATE:This approach is NOT working for me. It fails on blobframe table creation with the following field definition (don't know the reason, though):

更新:这种方法对我不起作用。它在使用以下字段定义创建 blobframe 表时失败(虽然不知道原因):

@Lob
@Column(name="blobImg", columnDefinition="bytea")
private byte[] blobImg;

Thank you Ernusc for your quick response.

感谢 Ernusc 的快速回复。

回答by Ernusc

If you want to convert your byte[]to sql type Blob, you can do the following:

如果要将您的转换byte[]为 sql 类型Blob,您可以执行以下操作:

@Lob(type = LobType.BLOB)

or

或者

@Type(type = "org.hibernate.type.BlobType")
@Lob

Hope it helps

希望能帮助到你