Spring Boot Data Persistence with JPA 5 — Questions and Answers
Question 1: Which Spring Data JPA keyword in a method name generates an IS NULL condition?
- IsNull (Correct answer)
- Null
- EqualsNull
- WhereNull
Correct answer: IsNull
Appending `IsNull` or `Null` to a field name in a repository method name generates a WHERE field IS NULL clause.
Question 2: What does `@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"email"}))` accomplish?
- Adds a unique index at the JPA layer only
- Adds a unique database constraint on the email column during schema generation (Correct answer)
- Makes the email field the primary key
- Prevents duplicate email values in memory only
Correct answer: Adds a unique database constraint on the email column during schema generation
uniqueConstraints tells JPA schema generation to create a UNIQUE constraint on the specified column(s) in the database.
Question 3: Which method on `EntityManager` is used to synchronize a detached entity back to the persistence context?
- persist()
- merge() (Correct answer)
- refresh()
- attach()
Correct answer: merge()
merge() copies the state of a detached entity into a new managed instance and returns that managed instance.
Question 4: What problem does the N+1 query issue describe in JPA?
- Running one bulk insert that generates N individual INSERTs
- Fetching a list of N entities triggers N additional queries for their lazy associations (Correct answer)
- Using N indexes causes one slow query
- A single entity mapped to N+1 columns
Correct answer: Fetching a list of N entities triggers N additional queries for their lazy associations
N+1 occurs when loading a parent list (1 query) then triggering a separate query for each child association (N queries), totaling N+1 queries.
Question 5: Which JPQL keyword is used to eagerly fetch associations in a single query to avoid N+1?
- INNER JOIN
- JOIN FETCH (Correct answer)
- EAGER JOIN
- LEFT FETCH
Correct answer: JOIN FETCH
JOIN FETCH in JPQL instructs Hibernate to include the association in the same SQL SELECT, preventing additional lazy-load queries.
Question 6: Which annotation on a Spring service method ensures it runs within an existing transaction or creates a new one if none exists?
- @Transactional(propagation = Propagation.REQUIRES_NEW)
- @Transactional(propagation = Propagation.REQUIRED) (Correct answer)
- @Transactional(propagation = Propagation.SUPPORTS)
- @Transactional(propagation = Propagation.NEVER)
Correct answer: @Transactional(propagation = Propagation.REQUIRED)
Propagation.REQUIRED (the default) joins an active transaction or starts a new one, ensuring the method always runs transactionally.
Question 7: What does `spring.jpa.show-sql=true` do in a Spring Boot application?
- Enables the H2 web console
- Logs all SQL statements generated by Hibernate to the console (Correct answer)
- Prints entity schema on startup
- Activates SQL validation mode
Correct answer: Logs all SQL statements generated by Hibernate to the console
Setting show-sql to true instructs Hibernate to print every generated SQL statement to standard output, useful for debugging.
Which Spring Data JPA keyword in a method name generates an IS NULL condition?