Scrivio Interview library

Queries

32 questions in 10 subtopics.

JPQL

ConceptFoundations6 min read
How does JPQL differ from SQL? Write a JPQL query that returns customers in a given city who have at least one order above an amount.
DebuggingFoundations6 min read
query.getSingleResult() sometimes throws NoResultException and sometimes NonUniqueResultException in production. What does this tell you, and how should the code handle it?
ConceptIntermediate6 min read
What query features did Hibernate 6 and Jakarta Persistence 3.2 add to HQL and JPQL that previously required native SQL?
LeadershipPrincipal6 min read
You are asked whether your company should standardise on ORM-based data access (JPA) or SQL-first tools (JdbcClient, jOOQ, MyBatis) for the next decade. How do you frame the decision?

Criteria API

CodingFoundations6 min read
Write a Criteria API query that finds products with an optional name filter and an optional maximum price.
ScenarioIntermediate7 min read
Your team writes dynamic search with Criteria API code that has become hard to read and test. What alternatives and structuring approaches would you consider?
DesignAdvanced7 min read
Your product needs an advanced search screen with 25 optional filters over several joined tables, used by thousands of users. Would you build it on the JPA Criteria API or something else? Explain the decision.

Native queries

ConceptFoundations7 min read
When is a native SQL query appropriate in a JPA application, and what do you lose compared with JPQL?
CodingIntermediate6 min read
Write a Spring Data JPA native upsert for PostgreSQL that increments a counter row or inserts it if missing, and explain the transactional and persistence-context implications.
LeadershipAdvanced6 min read
A service has grown to 200 native queries in JPA repositories, many with copy-pasted fragments. Should the team move to a SQL-first tool such as jOOQ, back to JPQL, or keep them? How do you decide?
ScenarioPrincipal6 min read
Your services use many database-specific native queries. The company is considering a move to a different database engine for cost reasons. How do you estimate how locked in you are and what the move would cost?

Pagination with joins

CodingFoundations6 min read
Write a Spring Data JPA method that returns the second page of 20 orders for a customer, newest first, including a stable tiebreaker.
ConceptIntermediate7 min read
Why can the count query that Spring Data derives for a paged @Query with joins give wrong totals or be slow, and how do you fix it?
DebuggingIntermediate6 min read
A repository method with @Query("select o from Order o join fetch o.lines") and a Pageable logs 'HHH90003004: firstResult/maxResults specified with collection fetch; applying in memory' and the service uses lots of memory. Explain and fix.

Bulk updates

CodingFoundations5 min read
Write a Spring Data JPA bulk update that deactivates users who have not logged in since a date, and explain what @Modifying(clearAutomatically = true) does.
DebuggingIntermediate6 min read
After a bulk JPQL update of prices, optimistic locking stops detecting conflicting edits of products, and entity listeners for auditing did not run. Explain.
ScenarioAdvanced6 min read
A team regularly runs large bulk updates and deletes through @Modifying queries on shared tables, and other services report lock waits and stale caches. What guidance would you give?

Query hints

CodingFoundations5 min read
How do you apply a read-only hint and a query timeout to a Spring Data JPA repository method?
ConceptIntermediate6 min read
Name useful JPA and Hibernate query hints and what each does: read-only, fetch size, timeout, lock timeout and comment.
ScenarioAdvanced6 min read
A runaway query from one service locked up a shared database for 20 minutes. What timeout and hint policies would you introduce across services?
LeadershipPrincipal6 min read
Over the years, teams have added database-specific hints, plan guides and tuning parameters to fix slow queries. How do you prevent these local fixes from becoming long-term liabilities?

Named queries

ConceptFoundations5 min read
What are JPA named queries (@NamedQuery), when are they validated, and how does Spring Data JPA pick them up for a repository method?
ScenarioIntermediate6 min read
A legacy module has 300 @NamedQuery annotations spread over entities, some unused and some duplicated with small differences. How would you clean this up safely?

Stored procedure calls

CodingFoundations6 min read
Show how to call a database stored procedure calculate_discount(IN customer_id BIGINT, OUT discount NUMERIC) from Spring Data JPA and from plain JPA.
DebuggingIntermediate6 min read
A Spring service calls a stored procedure through @Procedure inside a @Transactional method. The procedure commits internally, and when the Java code later fails, half the data is committed. Explain and fix.
DesignAdvanced6 min read
A bank's Java services call 150 stored procedures that hold core business rules. New teams want to move logic into Java. How do you decide which procedures to keep and how to manage them?

Full-text search with Hibernate Search

ConceptFoundations6 min read
What does Hibernate Search do, and how does it keep a Lucene or Elasticsearch index in sync with JPA entities?
ScenarioIntermediate6 min read
With Hibernate Search on Elasticsearch, index updates are sometimes lost when the application crashes right after commit, and bulk updates with JPQL do not reach the index. How would you address both?
DesignAdvanced6 min read
Should your teams use Hibernate Search, PostgreSQL full-text search (tsvector with GIN), or a separate search service fed by change data capture? Give criteria for each.

Type-safe query libraries: jOOQ and Querydsl

CodingFoundations6 min read
Write a jOOQ query that selects the ids and totals of orders over 100 for a customer, newest first, limited to 20, using generated classes. What does jOOQ check at compile time that JPQL strings do not?
ScenarioIntermediate6 min read
A team uses Querydsl with Spring Data for dynamic filters. The upstream Querydsl project has had long periods of slow maintenance and a community fork exists. What do you do about it?
LeadershipPrincipal7 min read
jOOQ is commercially licensed for commercial databases such as Oracle and SQL Server while open source for open source databases. As you set the company standard for data access libraries, how do licensing, lock-in and productivity factor in?