java - 使用带有 spring 的 import.sql 将图像存储为 byte[]

标签 java sql spring postgresql

我正在尝试在加载 spring 时使用 import.sql 文件将图像文件添加到数据库中。我目前正在使用 postgresql

我的字节数组在我的项目类中:

 @Lob
 private byte[] bytes;

我试图通过 pg_read_file 导入文件,如下所示:

-- Insert into Project

insert into project (title, colour, description, project_user_id, bytes) values ('Test', '#F6D2B4', 'a short description of some project, making sure its over 50 characters', 1, pg_read_file('classpath:static/images/pon9s2N.jpg'))

但是在运行spring的时候出现如下错误:

2018-04-29 10:33:43.812 ERROR 540 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000388: Unsuccessful: insert into project (title, colour, description, project_user_id, bytes) values ('Test', '#F6D2B4', 'a short description of some project, making sure its over 50 characters', 1, pg_read_file('classpath:static/images/pon9s2N.jpg'))

2018-04-29 10:33:43.813 ERROR 540 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: column "bytes" is of type oid but expression is of type text
Hint: You will need to rewrite or cast the expression.
Position: 179

我试过使用 CAST(pg_read... AS byte[]) 但没有成功。

谢谢。

最佳答案

使用pg_read_binary_file读取二进制文件。

关于java - 使用带有 spring 的 import.sql 将图像存储为 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50085255/

相关文章:

java - 在Spring上使用Jersey时从HK2收到“找不到合适的构造函数”

java - 如何在UART(atmega16)编码中添加AT命令以向手机发送短信?

spring - 如何创建 Gradle 任务以在 Spring 中仅运行特定的测试

java - Log4j(通过 Spring Boot Logging)logging.level 属性中是否允许使用通配符?

java - 打印出用户输入的数组

PHP通过主键返回查询的结果集

c# - 通过 C# 使用 ado.net 将值插入 SQL Server 数据库

mysql - SQL多表查询速度慢

java - 在 aspect 中读取注释属性

java - 无法在 null 问题上找到属性或字段