Skip to content

ClassCastException in JpaRepository method that returns Stream<> when running in native image #30176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andrey-nakin opened this issue Mar 23, 2023 · 2 comments
Labels
for: external-project Needs a fix in external project status: duplicate A duplicate of another issue

Comments

@andrey-nakin
Copy link

andrey-nakin commented Mar 23, 2023

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 [email protected]/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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 23, 2023
@sbrannen sbrannen changed the title ClassCastException in JpaRepository method that returns Stream<> when using Spring Native ClassCastException in JpaRepository method that returns Stream<> when running in native image Mar 23, 2023
@christophstrobl
Copy link
Member

This seems to be a duplicate of spring-projects/spring-data-jpa#2848.

@sbrannen
Copy link
Member

Indeed, this does look like a duplicate of that issue.

Thanks for the tip, @christophstrobl.

In light of that, I am closing this issue.

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Mar 23, 2023
@sbrannen sbrannen added status: duplicate A duplicate of another issue for: external-project Needs a fix in external project and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project Needs a fix in external project status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants