Hibernate / hsqldb 2无法水合Blob列
作者:互联网
我正在尝试使用Hibernate 3.5.6从HSQL 2.0数据库加载带有字节数据(用@Lob注释)的实体.此实体可以保存而没有任何问题,并且如果它在缓存中(即不需要水合),则可以很好地加载.但是,当实体不在缓存中(需要水合)时,我收到以下异常:
Caused by: org.hsqldb.HsqlException: incompatible data type in conversion: from SQL type BLOB to [B, value: instance of org.hsqldb.types.BlobDataID
at org.hsqldb.error.Error.error(Unknown Source)
... 68 more
这是完整堆栈跟踪(减去某些特定于域的跟踪),以获取更多上下文:
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1235)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
...
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2452)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
at org.hibernate.loader.Loader.list(Loader.java:2187)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:241)
... 45 more
Caused by: java.sql.SQLSyntaxErrorException: incompatible data type in conversion: from SQL type BLOB to [B, value: instance of org.hsqldb.types.BlobDataID
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.getColumnInType(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.getBytes(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.getBytes(Unknown Source)
at org.hibernate.type.AbstractBynaryType.get(AbstractBynaryType.java:103)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:186)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:175)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2267)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1443)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1371)
at org.hibernate.loader.Loader.getRow(Loader.java:1271)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:619)
at org.hibernate.loader.Loader.doQuery(Loader.java:745)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
... 53 more
Caused by: org.hsqldb.HsqlException: incompatible data type in conversion: from SQL type BLOB to [B, value: instance of org.hsqldb.types.BlobDataID
at org.hsqldb.error.Error.error(Unknown Source)
... 68 more
当我使用Hibernate 3.5.6和HSQL 1.8.0.10时,不会发生此问题,可能是由于所使用的方言不同(HSQL 1.8中不支持Blob列).我使用的Hibernate版本应该支持HSQL 2,并且我已经检查它是否可以正确检测HSQL版本并使用正确的方言.当我使用MySQL时,也不会发生此问题.
有问题的实体具有一个Lob列,其设置如下:
@Entity
public class ImageEntity extends IdEntity {
@Lob
@Column(name="IMAGE")
private byte[] imageBytes;
...
这是Hibernate / HSQL 2.0错误吗?
解决方法:
我在JDBCResultSet.java(从Sourceforge项目下载的HSQLDB 2.0.0)getBytes方法中找到了问题. HSQLDB 2.0支持一个不包含1.8.*的blob列,因此在它们没有完全更新其JDBC实现时可能是一个错误.以下对getBytes方法的修补程序解决了该问题:
public byte[] getBytes(int columnIndex) throws SQLException {
Type sourceType = resultMetaData.columnTypes[columnIndex-1];
if (sourceType.isLobType()){
Blob b = getBlob(columnIndex);
return b.getBytes(1, (int)b.length());
}
Object x = getColumnInType(columnIndex, Type.SQL_VARBINARY);
if (x == null) {
return null;
}
return ((BinaryData) x).getBytes();
}
原始方法是:
public byte[] getBytes(int columnIndex) throws SQLException {
Object x = getColumnInType(columnIndex, Type.SQL_VARBINARY);
if (x == null) {
return null;
}
return ((BinaryData) x).getBytes();
}
标签:hibernate,jpa-2-0,hsqldb,java 来源: https://codeday.me/bug/20191208/2093453.html