1

I have a simple entity

@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();
}

As you can see I want to use POJO as result for by native named query and use it in Spring data JPA repository. When I call the repository method I get the following exception:

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

I spend the whole day to play with configuration nothing helps. Any ideas what I do incorrectly?

BTW,

It should be simple based on an article like this http://www.thoughts-on-java.org/result-set-mapping-constructor-result-mappings/.

I use Spring Data JPA 1.9.5.RELEASE, Hibernate 5.1.0.Final, Hibernate-Jpa-api 2.1.

Jsr310JpaConverters is added to @EntityScan.

After debugging I've noticed that in my case Hibernate uses org.hibernate.type.SerializableType with reference to java.time.LocalDate and couldn't deserialize result value to LocalDate, but for other cases Hibernate uses org.hibernate.type.DateType with reference to java.time.LocalDate and it works for these cases (e.g. not named native queries).

1
  • e.g. If I replace LocalDate by String everywhere this configuration works. Commented Oct 21, 2016 at 23:07

1 Answer 1

4

Use Hibernate type corresponding to java's LocalDate as below :

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

More info: https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html

Sign up to request clarification or add additional context in comments.

1 Comment

I will check it and let you know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.