I have a JPA repository with the following method:
public interface SampleRepository extends JpaRepository<SampleEntity, Integer> {
Stream<SampleEntity> findAllByOrderByName();
...
The method is used in a REST controller as follows:
@GetMapping("/samples/first")
@Transactional(readOnly = true)
public SampleEntity first() {
return repository.findAllByOrderByName().findFirst().orElseThrow();
In works well when my application is running as a regular Java app under JRE. But when I build it into a Docker image using Spring Native, the invocation of findFirst()
in the code above causes ClassCastException
:
java.lang.ClassCastException: com.example.demo.entity.SampleEntity cannot be cast to java.lang.Object[]
at org.springframework.data.jpa.provider.PersistenceProvider$HibernateScrollableResultsIterator.next(PersistenceProvider.java:394) ~[na:na]
at java.base@17.0.6/java.util.Spliterators$IteratorSpliterator.tryAdvance(Spliterators.java:1856) ~[com.example.demo.DemoApplication:na]
The workaround is to change the repository method's return value from Stream<>
to List<>
.
Please refer to spring-native-issues repo with a complete demo. Make sure stream-first
branch of the repo is used.
Comment From: christophstrobl
This seems to be a duplicate of spring-projects/spring-data-jpa#2848.
Comment From: sbrannen
Indeed, this does look like a duplicate of that issue.
Thanks for the tip, @christophstrobl.
In light of that, I am closing this issue.