Spring Data JPA
25 questions in 7 subtopics.
Repository interfaces
Explain the Spring Data repository hierarchy (Repository,CrudRepository, ListCrudRepository, PagingAndSortingRepository, JpaRepository) and how a repository interface gets an implementation at runtime.How does SimpleJpaRepository.save() decide between persist and merge, and why can saving an entity with an assigned id cause an extra SELECT?A developer calls repository.deleteById(id) for 10,000 ids in a loop and it takes minutes. What SQL runs, and what are faster options?Should repositories be exposed directly to controllers or other modules? Describe the layering rules you would set for Spring Data repositories in a modular monolith.Your company is deciding whether new services should use Spring Data JPA, Spring Data JDBC, a SQL-first library such as jOOQ, or a mix. Frame the decision for a ten-year horizon, including skills, upgrade cost and vendor risk.Derived query methods
What SQL would Spring Data JPA generate forList<User> findTop10ByStatusAndCreatedAtAfterOrderByCreatedAtDesc(Status s, Instant t), and what happens if you misspell a property name?A derived method void deleteByStatus(Status s) on a JpaRepository fails with 'No EntityManager with actual transaction available for current thread' when called from a non-transactional service. Why, and what does it actually do when it works?Explain existsBy, countBy and the projection return types of derived queries. How can you make an existence check efficient?Convenience features such as derived query methods let teams ship quickly but hide data access details. How would you balance delivery speed and long-term maintainability in data access across a large engineering organisation?@Query
Write a@Query method with named parameters that finds orders by status and minimum total, and explain when the @Param annotation is needed.Write a paged native query with @Query in Spring Data JPA and explain why you may need countQuery. Mention any newer annotation for native queries.A large codebase has hundreds of @Query strings with duplicated filters and no tests. Schema changes break them only at runtime in some environments. What would you put in place?Paging and sorting
What is the difference between Page, Slice and List as return types for a paged repository method in Spring Data?Implement keyset (scrolling) pagination with Spring Data JPA's Window andScrollPosition API for a feed of posts ordered by createdAt and id.A public endpoint accepts sort=anyProperty and size=anything, passed straight to Spring Data. What risks does this create and what guardrails would you add?Your data volumes grew a hundredfold over eight years, and paging and sorting choices that worked early (offset paging, sorting on any field, exact totals) now cause most of your slow queries. What would you change in standards and in existing systems?Specifications and Querydsl
Write Spring Data JPA Specifications for 'status equals' and 'created after' and combine them in a repository call.Compare Spring Data Specifications and Querydsl for dynamic queries in a long-lived project. What would make you pick one over the other?Your platform depends heavily on a query library whose maintenance has slowed. How do you assess and manage the risk of a core data access dependency becoming unmaintained?Auditing
Set up Spring Data JPA auditing so entities getcreatedAt, updatedAt and createdBy filled automatically.A regulator requires a complete change history for customer records: who changed what and when, for seven years. Compare Spring Data auditing, Hibernate Envers, database triggers and change data capture.Several services each implement auditing differently (Spring Data annotations, Envers, custom tables, logs). Design a consistent auditing approach for the organisation.Custom repository implementations
Add a custom method with hand-written JPQL or Criteria code to a Spring Data repository. Show the interfaces, the implementation class name and how Spring finds it.How would you add a soft-delete or tenant filter behaviour to all repositories in a service? Compare a custom base repository class with Hibernate filters and other options.Spring Data repositories have shaped how a generation of Java developers think about data access. What long-term risks and benefits do you see in making them the default abstraction across a large organisation?No question matches these choices.