Queries
32 questions in 10 subtopics.
JPQL
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.query.getSingleResult() sometimes throws NoResultException and sometimes NonUniqueResultException in production. What does this tell you, and how should the code handle it?What query features did Hibernate 6 and Jakarta Persistence 3.2 add to HQL and JPQL that previously required native SQL?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
Write a Criteria API query that finds products with an optional name filter and an optional maximum price.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?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
When is a native SQL query appropriate in a JPA application, and what do you lose compared with JPQL?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.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?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
Write a Spring Data JPA method that returns the second page of 20 orders for a customer, newest first, including a stable tiebreaker.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?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
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.After a bulk JPQL update of prices, optimistic locking stops detecting conflicting edits of products, and entity listeners for auditing did not run. Explain.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
How do you apply a read-only hint and a query timeout to a Spring Data JPA repository method?Name useful JPA and Hibernate query hints and what each does: read-only, fetch size, timeout, lock timeout and comment.A runaway query from one service locked up a shared database for 20 minutes. What timeout and hint policies would you introduce across services?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
What are JPA named queries (@NamedQuery), when are they validated, and how does Spring Data JPA pick them up for a repository method?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
Show how to call a database stored procedurecalculate_discount(IN customer_id BIGINT, OUT discount NUMERIC) from Spring Data JPA and from plain JPA.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.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
What does Hibernate Search do, and how does it keep a Lucene or Elasticsearch index in sync with JPA entities?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?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
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?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?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?No question matches these choices.