🗃️ Spring Data JPA and Hibernate — interview questions

12 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.

12 of 12

01A `@Transactional` service method loads an `Author`, calls `setName(...)`, and returns. There is no `save()` anywhere, yet the log shows `update Author set name=? where id=?`. Where did that UPDATE come from, and is adding `repository.save(author)` "to be safe" a good idea?Persistence contexteasy0-2y02Code adds a new `Book` to `author.getBooks()` and persists both. Every assertion in the method passes — `author.getBooks()` contains the book — but in the database `book.author_id` is `NULL`. No error was thrown. What went wrong?Mappingeasy0-2y03A repository declares `List<User> findByEmaill(String email)` — a typo. When does that fail: at compile time, at startup, on the first call, or never? And at what point should a derived query method stop being a method name?Querieseasy0-2y04An endpoint lists 500 authors with their books and takes eleven seconds; the code has no loop over queries anywhere. A teammate fixes it with `select a from Author a join fetch a.books`, and it is fast — but two authors have vanished from the list. Explain both problems.Fetchingmedium3-5y05A service returns an `Order`, and the controller calls `order.getLines()` and gets `LazyInitializationException: could not initialize proxy - no Session`. One teammate suggests making `lines` EAGER; another says just keep `spring.jpa.open-in-view=true`. What is the exception actually telling you, and what is the right fix?Persistence contextmedium3-5y06An entity's `equals`/`hashCode` are generated from its `@GeneratedValue` id. A new `Tag` is added to a `HashSet`, persisted, and then `set.contains(tag)` returns `false` — even though `set.size()` is 1 and iterating returns that exact object. Why, and what should an entity's `equals`/`hashCode` be based on?Mappingmedium3-5y07A `@Transactional` transfer debits one account and then throws a checked `InsufficientFundsException` from a later check. The caller catches it, and the money has moved anyway. Why did the transaction commit, and how do you make it roll back?Transactionsmedium3-5y08Inside one transaction, a service runs a repository `@Modifying @Query("update Account a set a.status = 'FROZEN' where a.owner = :owner")`, then calls `findById` on one of those accounts and still sees `ACTIVE`. The database says `FROZEN`. What happened, and what do `clearAutomatically` and `flushAutomatically` each fix?Querieshard5-8y09`Category.products` is a `@OneToMany(mappedBy = "category")`, and someone adds `orphanRemoval = true` "for consistency" with `Order.lines`, which has it. The next day, an admin un-categorises a product (removes it from the category's collection and clears `product.category`), and the product row is deleted from the database. No `remove()` was called anywhere. Explain the difference between `CascadeType.REMOVE` and `orphanRemoval`, and why only one of them fired here.Mappinghard5-8y10A migration `V5__add_index_on_orders.sql` has already run in production. A follow-up PR notices the index is on the wrong column and fixes the column name inside `V5`. The next deploy fails before the application starts. What exactly did Flyway check, and what should the PR have done instead? When is `flyway repair` the right tool?Migrationsmedium5-8y11`placeOrder()` is `@Transactional` (REQUIRED) and calls `audit.logAttempt()` on another bean, which is `@Transactional(propagation = REQUIRES_NEW)` so the audit row survives a failed order. Under load, with a pool of 10 connections, requests start hanging for 30 seconds and then failing with a connection-timeout error, while the database is nearly idle. Walk through what REQUIRES_NEW actually does and why this happens.Transactionssenior8+y12A long-running `@Transactional` job loads an `Account`, does some work, then re-runs a JPQL query for the same account "to get the latest balance" — the SQL shows in the log — and changes its `status`. Meanwhile another request committed a new balance. After the job commits, that other request's balance change is gone. Explain every step, and how `@Version` changes the outcome.Persistence contextsenior8+y