JPA performance
30 questions in 9 subtopics.
Lazy and eager loading
What are the default fetch types for@ManyToOne, @OneToOne, @OneToMany and @ManyToMany, and why is FetchType.EAGER usually a bad default?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?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
This code runs 101 queries for 100 orders:List<Order> orders = orderRepository.findAll();
orders.forEach(o -> System.out.println(o.getCustomer().getName()));
@BatchSize, hibernate.default_batch_fetch_size and FetchMode.SUBSELECT differ in how they reduce N+1 queries?N+1 problems keep reaching production in your team's services. How do you detect them automatically before release?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
Write a Spring Data repository method that loads an Order with its lines and customer in one query, using@Query and using @EntityGraph.What is the difference between a fetch graph and a load graph in JPA entity graphs?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
A controller returns an Order entity and Jackson serialization fails withLazyInitializationException: could not initialize proxy - no Session. Open session in view is disabled. Explain the cause and the proper fixes.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?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
What doesspring.jpa.open-in-view do, what is its default in Spring Boot, and why does Boot log a warning about it?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.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
How do you enable JDBC batching for inserts and updates in Hibernate with Spring Boot, and why might inserts still not be batched?Write a service method that imports 500,000 records with JPA efficiently, and explain each choice.A data import service built with Spring DatasaveAll() takes 6 hours for 20 million rows. The business needs it in under 30 minutes. How would you approach it?Projections and DTOs
Show two ways to load only id and name of customers into a DTO with Spring Data JPA, without loading full entities.When would you load entities, and when DTO projections, in a service? Give a rule your team can follow.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
How does the Hibernate second-level cache work, what are its concurrency strategies, and how does it differ from the query cache?After enabling the second-level cache in a service with 4 instances, users sometimes see stale data for minutes. What are the likely causes?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?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
How do you see the SQL that Hibernate executes, including bind parameter values, in a Spring Boot 3 application?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.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.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?No question matches these choices.