ebean icon indicating copy to clipboard operation
ebean copied to clipboard

Chained fetch() calls

Open martinkala1 opened this issue 5 months ago • 3 comments

Hello,

I ran into an issue where if I have an entity that has multiple @OneToMany relations, and I attempt to fetch all of them, i.e.

QPerson().fetch("cars", "*").fetch("pets", "*").where().lastName.ieq("somedude").findOne()

only the first fetch gets executed, and the following (one or more fetches) is ineffective and produces no error.

I isolated the issue from my project in a standalone repo for convenience: https://github.com/martinkala1/ebean-chained-fetch

Note, that I do not have a lot of experience working with ebean, so You may find places where the example is not up to standard, for which I apologize.

My understanding is, that this operation should successfully fetch all the related entities, or at least produce some error/warning. Is this intended behavior or am I perhaps missing something?

Thanks for Your time, Martin

martinkala1 avatar Jun 16 '25 11:06 martinkala1

Hello @martinkala1,

Ebean has several fetch strategies: https://ebean.io/docs/query/fetch

in general, ebean can only fetch one many-relation in one query. In your example it will buld select * from person left join cars ... which results in person x cars rows. If you will also join pets, the result may "explode"

Normally additional manys are loaded batch wise. You may try an explicit "fetchQuery" here. (although the docs say, that fetch is converted automatically to fetchQuery. But I'm unsure if this is correct or if this means, that everything is fetched at once)

rPraml avatar Jun 16 '25 17:06 rPraml

If the OneToMany collections are moved out of the constructor then it works.

I'm not sure why until I look closer. I'm not sure if this is kotlin specific yet.

On Tue, 17 Jun 2025, 5:40 am Roland Praml, @.***> wrote:

rPraml left a comment (ebean-orm/ebean#3642) https://github.com/ebean-orm/ebean/issues/3642#issuecomment-2977477874

Hello @martinkala1 https://github.com/martinkala1,

Ebean has several fetch strategies: https://ebean.io/docs/query/fetch

in general, ebean can only fetch one many-relation in one query. In your example it will buld select * from person left join cars ... which results in person x cars rows. If you will also join pets, the result may "explode"

Normally additional manys are loaded batch wise. You may try an explicit "fetchQuery" here. (although the docs say, that fetch is converted automatically to fetchQuery. But I'm unsure if this is correct or if this means, that everything is fetched at once)

— Reply to this email directly, view it on GitHub https://github.com/ebean-orm/ebean/issues/3642#issuecomment-2977477874, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABTATP5X3TUP6EUIVB24K33D36PTAVCNFSM6AAAAAB7NAKUSGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSNZXGQ3TOOBXGQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

rbygrave avatar Jun 17 '25 07:06 rbygrave

Hello, https://github.com/ebean-orm/ebean/issues/3642#issuecomment-2979323949 fixes it, thanks!

martinkala1 avatar Jun 17 '25 09:06 martinkala1

I'll close this one now.

rbygrave avatar Aug 31 '25 10:08 rbygrave