Joins not pulling related 1:1 mapping

19 views
Skip to first unread message

Northwes

unread,
Feb 8, 2018, 4:08:16 PM2/8/18
to Querydsl

I am using the spring data and the QueryDslPredicateExecutor repository pattern to generate most of my queries for me.

When I query for the entities, I search (with 3 or 4 Boolean expressions)

all of the queries are doing left outer joins, but the first query does not select the related entity back into memory.

In my case that first query returns 10 items.

The next 10 queries, for the related entity, also join back to the initial entity, but now the tools decide they can select other entities and it selects the props from the first entity. (Which it shouldn’t need to do at all!)

Ex. A is related to B. The first query for A finds 10.
That query joins to B but does not pull any properties of B through.

After this, 10 queries run for B all joining back to A and those queries pull properties of A (although hibernate should have it all cached)

Am I missing something or is this pretty crazy.

Richard Richter

unread,
Feb 9, 2018, 6:41:02 AM2/9/18
to Querydsl
Hi

Pity you didn't include some actual code. I suspect you're missing joinFetch instruction, which is necessary - at least in JPA (again, without code I may just assume you mean JPA and not native SQL).

Try something like:
      .leftJoin(QDog.dog.owner).fetchJoin() // fetchJoin necessary

See whether it makes any difference.

The thing is that in the select(A) you only say to fetch A. You could select(A, B) - returning a tuple, which would also fetch things at once, but dealing with tuple may be unwanted here. That .fetchJoin() after join should help.

For more about N+1 select problem: https://leanpub.com/opinionatedjpa/read#ch-avoid-n-plus-1

Cheers

Virgo
Reply all
Reply to author
Forward
0 new messages