我正在尝试使用Hibernate将pdf文档保存到我的postgres 10.1数据库中。 下面是我的实体类。
@Entity
@Table(name = "FILEUPLOADS", schema="SomeSchema")
public class FileUploads{
//skipping other columns
@Lob
@Column(name="DOCDATA")
private byte[] docData;
}
此列是postgres数据库中的操作类型 bytea 。
如Authur在这里建议的那样:similar issue,我创建了如下的postgres方言的自定义实现。
@Configuration
public class TestDialect extends PostgreSQLDialect{
public TestDialect() {
super();
registerColumnType(Types.BLOB, "bytea");
}
}
然后在我的persistence.xml文件中,我正在使用此自定义方言
<property name="hibernate.dialect" value="com.digital.repository.TestDialect" />
即使完成所有这些操作,我仍会遇到错误
ERROR: column "DOCDATA" is of type bytea but expression is of type bigint
Hint: You will need to rewrite or cast the expression.
Position: 251
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2468)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2211)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:309)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:446)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:370)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:149)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:124)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 250 more
要解决此问题,我还需要做什么?
答案 0 :(得分:1)
尝试更改休眠类型:
@Entity
@Table(name = "FILEUPLOADS", schema="SomeSchema")
public class FileUploads{
//skipping other columns
@Type(type = "org.hibernate.type.BinaryType")
@Column(name="DOCDATA")
private byte[] docData;
}
答案 1 :(得分:0)
下面的配置对我有用,现在我可以将doc / pdf保存到数据库中。
@Configuration
public class CustomPostgresDialect extends PostgreSQL82Dialect{
@Override
public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor) {
if (sqlTypeDescriptor.getSqlType() == java.sql.Types.BLOB) {
return BinaryTypeDescriptor.INSTANCE;
}
return super.remapSqlTypeDescriptor(sqlTypeDescriptor);
}
}
休眠4.1.10
Java 1.7