Java Developer Interview Question

Your endpoint issues hundreds of queries per request. How does that happen with JPA and how do you fix it?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

That is the N plus one problem: one query loads the parent rows, then touching a lazy association on each one triggers another query per row. Fix it by fetching the association in the same query with a join fetch or an entity graph, by setting a batch fetch size so Hibernate loads associations in groups, or by projecting straight into a DTO with only the columns you need. Keep associations lazy by default and fetch eagerly per use case.

Why interviewers ask this

It is the most common performance bug in Spring and Hibernate applications, so the interviewer wants to know you can recognize it from the logs and fix it without turning everything eager. They want to hear that fetch strategy belongs to the query rather than the mapping, plus awareness of the related issues: lazy initialization outside a session, open session in view, and Cartesian products from joining two collections.

How to structure your answer

  • Name the pattern and explain what triggers the extra queries.
  • Give the fixes in order: join fetch, entity graph, batch size, projection.
  • Insist that mappings stay lazy and queries decide the fetching.
  • Mention detection: log the SQL or assert the query count in tests.

Example answer

Spoken example, first person

It is N plus one. I load a page of orders in one query, then the serializer touches order.getItems on each one, and because the association is lazy Hibernate goes back for every single order. Twenty orders becomes twenty one queries, and it only shows up under real data. The first fix is to fetch what I need in one go, either a query with a join fetch or a named entity graph on the repository method, so the items come back with the parents. If I need two separate collections I do not join both, because that multiplies rows into a Cartesian product; instead I set a batch fetch size so Hibernate loads them in batches of, say, fifty with an in clause, which is two queries instead of a hundred. Often the real answer is that the endpoint does not need entities at all, so I project directly into a DTO with just the columns required. What I do not do is switch the mapping to eager, because that fixes one endpoint and slows down every other one. I keep it honest by asserting query counts in an integration test.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • What is a LazyInitializationException and how do you avoid it properly?
  • Why is open session in view considered harmful?
  • How would you page a query that also join fetches a collection?

Related java developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot