java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object; while using JPA 2.2 query.getResultStream().findFirst()

Description

The code of my Spring Data repository method is as follows:

As you can see, I am attempting to fetch, as byte array, the shipmentLabel column, defined in my Postgres schema as bytea. The following exception occurs at runtime:

java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object; at org.hibernate.internal.ScrollableResultsImpl.prepareCurrentRow(ScrollableResultsImpl.java:203) at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:101) at org.hibernate.query.internal.ScrollableResultsIterator.hasNext(ScrollableResultsIterator.java:33) at java.util.Spliterators$IteratorSpliterator.tryAdvance(Spliterators.java:1811) at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126) at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:499) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:486) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:531) at org.hibernate.query.spi.StreamDecorator.findFirst(StreamDecorator.java:260)

By looking into the code in org.hibernate.internal.ScrollableResultsImpl at line 203, it attempts to cast the primitive byte[] to Object[].

org.hibernate.internal.ScrollableResultsImpl

Is this the intended behaviour?

As an alternative, I can use query.getResultList().stream().findFirst().

Later edit: the shipmentLabel field is mapped as org.hibernate.type.BinaryType.

 

Thank you for your help.

Environment

AdoptOpenJDK build 1.8.0_265-b01, Spring Boot 2.3.3, Hibernate 5.4.20, postgresql JDBC driver version 42.2.14, PostgreSQL 10.11

Activity

Show:
Nathan Xu
September 26, 2020, 2:59 AM
Edited

PR created at: . Thanks for the ticket!

Dragoş Haiduc
September 28, 2020, 6:40 AM

Thank you also!

Assignee

Nathan Xu

Reporter

Dragoş Haiduc

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure