@SqlResultSetMapping @ConstructorResult @ColumnResult对java.time.LocalDate不起作用

时间:2016-10-21 22:48:42

标签: hibernate java-8 spring-data-jpa jpa-2.1 sqlresultsetmapping

我有一个简单的实体

@Entity
@NamedNativeQueries({
    @NamedNativeQuery(name = "Account.dailyRegistered",
        query = "select date(date_trunc('day', creation_date)) as period, count(1) as metric"
            + "    from account "
            + "group by period "
            + "order by period",
        resultSetMapping = "PeriodMetric.byDay")
})
@SqlResultSetMappings({
    @SqlResultSetMapping(
        name = "PeriodMetric.byDay",
        classes = {
            @ConstructorResult(
                targetClass = PeriodMetric.class,
                columns = {
                    @ColumnResult(name = "period", type = java.time.LocalDate.class),
                    @ColumnResult(name = "metric", type = Integer.class)
                }
            )
        }
    )
})
public class Account {...}



public class PeriodMetric {
    private LocalDate period;
    private Integer metric;

    public PeriodMetric(LocalDate period, Integer metric) {
        ...
    }
   ...
}

public interface AccountRepository extends JpaRepository<Account, Long> {
    @Query(name = "Account.dailyRegistered", nativeQuery = true)
    List<PeriodMetric> findDailyRegistered();
}

正如您所看到的,我希望使用POJO作为本机命名查询的结果,并在Spring数据JPA存储库中使用它。 当我调用存储库方法时,我得到以下异常:

Caused by: org.hibernate.type.SerializationException: could not deserialize
    at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:243)
    at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:287)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:138)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:113)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:27)
    at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:60)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:235)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:231)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:227)
    at org.hibernate.loader.custom.ScalarResultColumnProcessor.extract(ScalarResultColumnProcessor.java:54)
    at org.hibernate.loader.custom.ConstructorResultColumnProcessor.extract(ConstructorResultColumnProcessor.java:58)
    at org.hibernate.loader.custom.ResultRowProcessor.buildResultRow(ResultRowProcessor.java:83)
    at org.hibernate.loader.custom.ResultRowProcessor.buildResultRow(ResultRowProcessor.java:60)
    at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:414)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:756)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
    at org.hibernate.loader.Loader.doQuery(Loader.java:930)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
    at org.hibernate.loader.Loader.doList(Loader.java:2610)
    at org.hibernate.loader.Loader.doList(Loader.java:2593)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2422)
    at org.hibernate.loader.Loader.list(Loader.java:2417)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:336)
    at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1980)
    at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:322)
    at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:125)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
    at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:529)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:206)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:78)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:100)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:91)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:462)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:440)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    ... 7 more
Caused by: java.io.StreamCorruptedException: invalid stream header: 32303136
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:309)
    at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:299)
    at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:218)
    ... 49 more

我花了整整一天玩配置没什么帮助。我有什么不正确的想法吗?

顺便说一句,

基于像http://www.thoughts-on-java.org/result-set-mapping-constructor-result-mappings/这样的文章应该很简单。

我使用Spring Data JPA 1.9.5.RELEASE,Hibernate 5.1.0.Final,Hibernate-Jpa-api 2.1。

Jsr310JpaConverters被添加到@EntityScan。

调试后我发现在我的情况下,Hibernate使用 org.hibernate.type.SerializableType 来引用java.time.LocalDate,并且无法将结果值反序列化为LocalDate ,但对于其他情况,Hibernate使用 org.hibernate.type.DateType 并引用java.time.LocalDate,它适用于这些情况(例如,未命名的本机查询)。

1 个答案:

答案 0 :(得分:4)

使用与java的LocalDate对应的Hibernate类型,如下所示:

@ColumnResult(name = "period", type = LocalDateType.class),

更多信息:https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html