Code RoomLazy loading N+1 queries
MediumPrep Room Coding #1840

Lazy loading N+1 queries

Code reviewCode quality & reviewMid–Senior~25 min

Review this Java (JPA/Hibernate) service method.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 25 min
Mark a line and say what kind of problem it is.0 findings
1@Transactional(readOnly = true)
2public List<TeamDto> activeTeams() {
3 List<Team> teams = em.createQuery(
4 "SELECT t FROM Team t WHERE t.active = true", Team.class)
5 .getResultList();
6 return teams.stream().map(t -> new TeamDto(
7 t.getName(),
8 t.getMembers().size(), // members is @OneToMany(fetch = LAZY)
9 t.getLead().getEmail() // lead is @ManyToOne(fetch = LAZY)
10 )).collect(Collectors.toList());
11}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.