Interview replay

Full round replay — fetch types

10 minintermediate115 yrs

Timed verbal replay with pass/fail criteria per follow-up.

How to run this

Answer out loud, timed. Do not read the entry first. Then compare against "The Answer" and "Interviewer's Next Move" and mark yourself.

The opener

What are the fetch types, and which is the default for each mapping?

Budget: 45 seconds. Going long here is itself a fail signal.

Follow-ups

  1. 1. “Which mappings default to EAGER?

    Testing: The single most misremembered fact here.

    Scoring

    Pass: @ManyToOne and @OneToOne. Collections — @OneToMany and @ManyToMany — default to LAZY. So loading one entity that touches nothing can still issue several queries.

    Fail: Says everything defaults to lazy.

  2. 2. “Why did they choose that?

    Testing: Whether they can justify it before criticising it.

    Scoring

    Pass: A to-one is one row, so fetching it was assumed cheap. That holds for loading a single entity and breaks immediately for a list — a hundred orders with three eager to-ones is three hundred extra queries before any code runs.

    Fail: Calls it arbitrary.

  3. 3. “What causes LazyInitializationException?

    Testing: The mechanism, not the symptom.

    Scoring

    Pass: Touching a lazy association after the persistence context closed. The field holds a proxy with nothing to load from — usually because the entity escaped the service layer and the view asked for something the service never fetched.

    Fail: Blames Hibernate, or says the data was missing.

  4. 4. “How would you fix it?

    Testing: Whether they reach for the right lever.

    Scoring

    Pass: Fetch what the endpoint needs while the session is open — @EntityGraph or a join fetch — or return a DTO so no entity crosses the boundary. Not by making the association eager, and not by widening the transaction.

    Fail: Sets EAGER, or adds @Transactional to the controller.

  5. 5. “What does spring.jpa.open-in-view do, and what is its default?

    Testing: A default most people have never looked at.

    Scoring

    Pass: Keeps the persistence context open until the response is written, so lazy loads during rendering succeed. It defaults to TRUE in Spring Boot, which warns about it on startup. That is why most developers rarely see the exception.

    Fail: Thinks it defaults to false, or has not heard of it.

  6. 6. “So is it good or bad?

    Testing: Judgement rather than a slogan.

    Scoring

    Pass: It hides a real cost. Queries move into rendering — after the transaction commits and after most instrumentation stops — so a service can measure one query while the request issues fifty. Turn it off and fix what breaks; each break was issuing unplanned queries.

    Fail: Calls it convenient with no downside, or calls it evil with no reason.

  7. 7. “Why doesn't @OneToOne(fetch = LAZY) work on the non-owning side?

    Testing: A specific, checkable fact.

    Scoring

    Pass: Hibernate has to know whether the related row exists to decide between a proxy and null, and it cannot know without querying. Fix it with optional = false, by owning the foreign key on that side, or with bytecode enhancement.

    Fail: Assumes LAZY always works.

  8. 8. “What happens if you return an entity from a controller?

    Testing: The most common real cause.

    Scoring

    Pass: Jackson touches every getter, including lazy ones. With open-in-view on it works and issues a query per association; with it off it throws mid-serialisation, leaving a half-written response. A bidirectional relationship also recurses. Return a DTO.

    Fail: Sees no problem, or reaches for @JsonIgnore.

  9. 9. “One policy for a new codebase?

    Testing: Whether they can commit.

    Scoring

    Pass: LAZY on every association, fetch deliberately per query, and open-in-view off. A lazy association can be fetched by a query that needs it; an eager one cannot be skipped by a query that does not.

    Fail: Says it depends, without saying on what.

Score yourself

9/9 — you can set the policy and defend it in a design review 7-8 — solid; the open-in-view and @OneToOne answers are where points go 4-6 — you know lazy from eager but not the defaults; redo the Warm-up 0-3 — count the queries for one entity load yourself

← Back to What are the fetch types, and which is the default for each mapping?