postgresql错误“org.postgresql.util.PSQLException: 不良的类型值 long ”解决方法:
以下是PostgreSQL抛出例外处的日志片段:
Caused by: org.postgresql.util.PSQLException: 不良的类型值 long : \x0040010346504d4e00000001000003900101000000000000000002800000028001f4007d000202040000000200000000000000000000000000000000000000005041 at org.postgresql.jdbc.PgResultSet.toLong(PgResultSet.java:2860) at org.postgresql.jdbc.PgResultSet.getLong(PgResultSet.java:2114) at org.postgresql.jdbc.PgResultSet.getBlob(PgResultSet.java:418) at org.postgresql.jdbc.PgResultSet.getBlob(PgResultSet.java:405) at org.apache.commons.dbcp.DelegatingResultSet.getBlob(DelegatingResultSet.java:565) at org.apache.commons.dbcp.DelegatingResultSet.getBlob(DelegatingResultSet.java:565) at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$1.doExtract(BlobTypeDescriptor.java:48) at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:258) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:254) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:244) at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:327) at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2775) at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.loadFromResultSet(EntityReferenceInitializerImpl.java:305) ... 64 more
解决方法:
在最底层的PostgreSQL81Dialect(藏的太深了),在getSqlTypeDescriptorOverride()接口中找到对应的内容,而且发现默认的oid + bigobject方式是在此定义的。于是重写该接口:
@Override public SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) { SqlTypeDescriptor descriptor; switch (sqlCode) { case Types.BLOB: // Force BLOB binding. Otherwise, byte[] fields annotated // with @Lob will attempt to use // BlobTypeDescriptor.PRIMITIVE_ARRAY_BINDING. Since the // dialect uses oid for Blobs, byte arrays cannot be used. //descriptor = BlobTypeDescriptor.BLOB_BINDING; descriptor = BlobTypeDescriptor.STREAM_BINDING; break; case Types.CLOB: //descriptor = ClobTypeDescriptor.CLOB_BINDING; descriptor = ClobTypeDescriptor.STREAM_BINDING; break; default: descriptor = super.getSqlTypeDescriptorOverride(sqlCode); break; } return descriptor; }
推荐使用二进制数组方式存取LOB,而不是默认的oid + bigobject;
如果抛错信息“不良的类型值”后是long,几乎可断定是LOB调用模式的问题;如果是其它类型,需进一步分析。
如果直接用JDBC,对LOB的调用接口是setBinaryStream()、setCharacterStream()等。
推荐:PostgreSQL教程