Skip to content
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

Merger applies all limit()-operators #353

Closed
julgus opened this issue Jun 30, 2023 · 1 comment
Closed

Merger applies all limit()-operators #353

julgus opened this issue Jun 30, 2023 · 1 comment
Assignees
Labels
bug Something isn't working
Projects
Milestone

Comments

@julgus
Copy link
Member

julgus commented Jun 30, 2023

Describe the bug
Limit is applied to the wrong Entities when using .flatMap().

Expected behavior
The following two streams should generate the same list of films:

List<String> expectedFilms = languages.stream()
              .sorted(Comparator.comparing(Language::getName))
              .flatMap(l -> l.getFilms().stream())
              .sorted(Comparator.comparing(Film::getLength))
              .map(Film::getTitle)
              .filter(t -> t.startsWith("A"))
              .limit(10)
              .collect(Collectors.toList());

     List<String> actualFilms = jpaStreamer.stream(of(Language.class).joining(Language$.films))
              .sorted(Language$.name)
              .flatMap(l -> l.getFilms().stream())
              .sorted(Film$.length)
              .map(Film$.title)
              .filter(t -> t.startsWith("A"))
              .limit(10)
              .collect(Collectors.toList());

Actual behavior
The list expectedFilms contains 10 films, and the list actualFilms contains 100 films. The generated query reveals that the .limit(10) is applied to the query that fetches languages instead of the flat-mapped Stream of films.

Hibernate: 
    /* <criteria> */ select
        l1_0.language_id,
        f1_0.language_id,
        f1_0.film_id,
        f1_0.description,
        f1_0.last_update,
        f1_0.length,
        f1_0.rating,
        f1_0.rental_duration,
        f1_0.rental_rate,
        f1_0.replacement_cost,
        f1_0.special_features,
        f1_0.title,
        l1_0.name 
    from
        language l1_0 
    left join
        film f1_0 
            on l1_0.language_id=f1_0.language_id 
    order by
        l1_0.name asc limit ?

How To Reproduce
Run the two Streams above.

Build tool
e.g. Maven 3.9.0

JPAStreamer version
e.g. JPAStreamer 3.0.2

JPA Provider
e.g. Hibernate 6.0.2.Final

Java Version
e.g. Java 17.0.19

@julgus julgus added the bug Something isn't working label Jun 30, 2023
@julgus julgus self-assigned this Jun 30, 2023
@julgus julgus added this to the 3.0.3 milestone Jun 30, 2023
@julgus julgus added this to Prio 1 in 3.0.4 Jun 30, 2023
@julgus
Copy link
Member Author

julgus commented Jul 10, 2023

Closing as this is related to #354.

@julgus julgus closed this as completed Jul 10, 2023
@julgus julgus changed the title Incorrect merge of the limit()-operator Merger applies all limit()-operators Jul 10, 2023
@julgus julgus moved this from Prio 1 to Done in 3.0.4 Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
3.0.4
Done
Development

No branches or pull requests

1 participant