Scrivio Interview library

JPA performance

30 questions in 9 subtopics.

Lazy and eager loading

ConceptFoundations6 min read
What are the default fetch types for @ManyToOne, @OneToOne, @OneToMany and @ManyToMany, and why is FetchType.EAGER usually a bad default?
InternalsIntermediate7 min read
How does Hibernate implement lazy loading for a @ManyToOne association and for a collection? What consequences does the proxy have for code like order.getCustomer().getClass() or instanceof checks?
LeadershipAdvanced6 min read
Your organisation's JPA entities were written with EAGER fetching everywhere ten years ago, and each change risks performance regressions. How would you plan the move to lazy-by-default across many teams?

N+1 problem

DebuggingFoundations6 min read
This code runs 101 queries for 100 orders:
List<Order> orders = orderRepository.findAll();
orders.forEach(o -> System.out.println(o.getCustomer().getName()));
Explain why and give three ways to fix it.
InternalsIntermediate6 min read
How do @BatchSize, hibernate.default_batch_fetch_size and FetchMode.SUBSELECT differ in how they reduce N+1 queries?
ScenarioIntermediate6 min read
N+1 problems keep reaching production in your team's services. How do you detect them automatically before release?
LeadershipPrincipal6 min read
Performance problems from ORM usage (N+1, over-fetching, chatty sessions) appear in most of your services years after launch. Is this an argument to move away from ORMs as an organisation, or to invest in using them better? How would you decide?

Fetch joins and entity graphs

CodingFoundations6 min read
Write a Spring Data repository method that loads an Order with its lines and customer in one query, using @Query and using @EntityGraph.
ConceptIntermediate6 min read
What is the difference between a fetch graph and a load graph in JPA entity graphs?
DebuggingIntermediate6 min read
A query join-fetches two collections, Order.lines and Order.payments, both Lists. It fails with MultipleBagFetchException. Changing both to Set makes it run, but it becomes very slow. Explain both behaviours and the right fix.

LazyInitializationException

DebuggingFoundations6 min read
A controller returns an Order entity and Jackson serialization fails with LazyInitializationException: could not initialize proxy - no Session. Open session in view is disabled. Explain the cause and the proper fixes.
ScenarioIntermediate6 min read
During code review you see Hibernate.initialize(order.getLines()) calls sprinkled in service methods, plus spring.jpa.open-in-view=true to avoid LazyInitializationException. What do you recommend for the codebase?
LeadershipAdvanced6 min read
LazyInitializationException is one of the most common production errors across your teams' services. What does that tell you about the codebase, and what systemic fix would you drive?

Open session in view

ConceptFoundations7 min read
What does spring.jpa.open-in-view do, what is its default in Spring Boot, and why does Boot log a warning about it?
DebuggingIntermediate7 min read
A service's connection pool saturates during traffic spikes, although transactions are short. Requests also call a slow external API after the service method returns, in the controller. OSIV is enabled. Explain the link.
LeadershipAdvanced6 min read
You want to switch off open session in view across 60 services where it has always been on. How do you plan the change to avoid production incidents?

Batching inserts and updates

ConceptFoundations6 min read
How do you enable JDBC batching for inserts and updates in Hibernate with Spring Boot, and why might inserts still not be batched?
CodingIntermediate7 min read
Write a service method that imports 500,000 records with JPA efficiently, and explain each choice.
ScenarioAdvanced7 min read
A data import service built with Spring Data saveAll() takes 6 hours for 20 million rows. The business needs it in under 30 minutes. How would you approach it?

Projections and DTOs

CodingFoundations6 min read
Show two ways to load only id and name of customers into a DTO with Spring Data JPA, without loading full entities.
DesignIntermediate6 min read
When would you load entities, and when DTO projections, in a service? Give a rule your team can follow.
LeadershipPrincipal7 min read
Over many years, your services have used JPA entities as the shape of API responses, events and reports. What has this cost, and what long-term data access policy would you set?

Second-level cache

ConceptIntermediate7 min read
How does the Hibernate second-level cache work, what are its concurrency strategies, and how does it differ from the query cache?
DebuggingIntermediate6 min read
After enabling the second-level cache in a service with 4 instances, users sometimes see stale data for minutes. What are the likely causes?
ScenarioAdvanced6 min read
Teams want to enable the Hibernate second-level cache widely to reduce database load. What would you approve and what would you steer them away from?
DesignPrincipal6 min read
Your company relies on several cache layers (Hibernate second-level cache, Redis, CDN) added over the years, and nobody can explain where stale data comes from. What long-term caching strategy and governance would you put in place?

Statement logging and analysis

ConceptFoundations6 min read
How do you see the SQL that Hibernate executes, including bind parameter values, in a Spring Boot 3 application?
DebuggingIntermediate7 min read
An endpoint's latency rose from 50 ms to 900 ms after a release. Describe how you would find which queries are responsible, from application logs to the database.
DesignAdvanced6 min read
Design fleet-wide observability for database access across 100 Java services so that slow queries and N+1 patterns are visible and attributable to a service and endpoint.
LeadershipPrincipal6 min read
Database cost is now one of your largest cloud bills, and it grows faster than traffic. How would you build long-term accountability for query efficiency across engineering?