使用 Postgresql 休眠未正确加载具有“文本”数据类型的列的数据

Posted

技术标签:

【中文标题】使用 Postgresql 休眠未正确加载具有“文本”数据类型的列的数据【英文标题】:Hibernate with Postgresql not loading the data correctly for column with 'text' datatype 【发布时间】:2019-05-08 04:26:06 【问题描述】:

我有以下实体,并且正在使用 liquibase loadData 加载一些示例数据

@Entity
class MyEntity 

    @Id
    private Long id;

    @Lob
    @Column(name="my_text")
    private String myText;

要使用 liquibase 加载示例数据,已使用以下 csv 文件。

id;my_text

1;referenceToFile

还有referenceToFile的内容

This is a very large text

liquibase 定义如下:

<changeSet id="123455-1-data" author="user">
    <loadData
              file="config/liquibase/data/myentity.csv"
              separator=";"
              tableName="myentity">
        <column name="id" type="numeric"/>
        <column name="my_text" type="blob"/>
    </loadData>
</changeSet>

但是,当使用休眠加载数据时,我收到以下错误。

Caused by: org.hibernate.exception.DataException: could not execute query
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:118)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
    at org.hibernate.loader.Loader.doList(Loader.java:2692)
    at org.hibernate.loader.Loader.doList(Loader.java:2672)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2506)
    at org.hibernate.loader.Loader.list(Loader.java:2501)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:504)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:395)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:220)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1507)
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1537)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)
    at org.hibernate.query.Query.getResultList(Query.java:132)
    at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:74)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:306)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:73)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
    at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    ... 165 common frames omitted
Caused by: org.postgresql.util.PSQLException: Bad value for type long : This is a very large text
    at org.postgresql.jdbc.PgResultSet.toLong(PgResultSet.java:2873)
    at org.postgresql.jdbc.PgResultSet.getLong(PgResultSet.java:2099)
    at org.postgresql.jdbc.PgResultSet.getClob(PgResultSet.java:459)
    at org.postgresql.jdbc.PgResultSet.getClob(PgResultSet.java:445)
    at com.zaxxer.hikari.pool.HikariProxyResultSet.getClob(HikariProxyResultSet.java)
    at org.hibernate.type.descriptor.sql.ClobTypeDescriptor$1.doExtract(ClobTypeDescriptor.java:44)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243)
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:329)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:3010)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1746)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1672)
    at org.hibernate.loader.Loader.getRow(Loader.java:1561)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:731)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:990)
    at org.hibernate.loader.Loader.doQuery(Loader.java:948)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
    at org.hibernate.loader.Loader.doList(Loader.java:2689)
    ... 196 common frames omitted

然后我发现了这个网站http://www.solewing.org/blog/2015/08/hibernate-postgresql-and-lob-string/,上面写着 postgres 将 text 存储在其他地方,并保留了 long 参考。但是 liquibase 似乎是直接插入文本值而不是遵循 postgres text 类型。

我的问题是我们如何才能使用 liquibase 为text 类型正确加载数据?

【问题讨论】:

***.com/questions/28588311/…的可能重复 【参考方案1】:

过去我也遇到过类似的问题,我用 clob 代替 blob,它解决了我的问题

【讨论】:

以上是关于使用 Postgresql 休眠未正确加载具有“文本”数据类型的列的数据的主要内容,如果未能解决你的问题,请参考以下文章

具有动态数据初始化的 UiTableviewCell 未正确加载

具有自定义数据库功能的休眠标准

休眠程序未终止

Qt 驱动程序 Postgresql 可用但未加载

使用返回表、休眠、postgresql 的函数

PostgreSQL 休眠和春季