Spring Boot Data Persistence with JPA 4 — Questions and Answers
Question 1: What exception does Spring's `@Transactional` mechanism translate a `ConstraintViolationException` into?
- DataIntegrityViolationException (Correct answer)
- IllegalStateException
- PersistenceException
- TransactionSystemException
Correct answer: DataIntegrityViolationException
Spring's persistence exception translation converts JPA/Hibernate ConstraintViolationException into DataIntegrityViolationException.
Question 2: Which `@OneToOne` fetch strategy is the default in JPA?
- FetchType.LAZY
- FetchType.EAGER (Correct answer)
- FetchType.AUTO
- FetchType.DEFERRED
Correct answer: FetchType.EAGER
JPA defaults @OneToOne (and @ManyToOne) to FetchType.EAGER, loading the related entity immediately.
Question 3: Which Spring Boot annotation enables JPA repositories scanning in a specific package?
- @EnableJpa
- @EnableJpaRepositories (Correct answer)
- @ComponentScan
- @JpaRepositoryScan
Correct answer: @EnableJpaRepositories
@EnableJpaRepositories triggers Spring Data JPA to scan for repository interfaces in the specified base package.
Question 4: What does `orphanRemoval = true` do on a JPA relationship?
- Cascades persist to orphaned records
- Automatically deletes child entities when they are removed from the parent collection (Correct answer)
- Prevents deletion of the parent if it has children
- Lazily loads orphaned child records
Correct answer: Automatically deletes child entities when they are removed from the parent collection
With `orphanRemoval = true`, JPA deletes a child entity from the database when it is removed from the parent's collection.
Question 5: Which JPQL clause filters query results equivalent to SQL's WHERE keyword?
- FILTER
- WHERE (Correct answer)
- HAVING
- ON
Correct answer: WHERE
JPQL uses the WHERE clause just like SQL to filter result sets based on conditions.
Question 6: What is a 'dirty check' in the context of JPA's persistence context?
- A security scan for unsafe SQL
- Automatic detection of changes to managed entities before flushing (Correct answer)
- A check for null fields before insert
- Validation of entity constraints at load time
Correct answer: Automatic detection of changes to managed entities before flushing
JPA compares the current state of managed entities to their snapshot at load time and generates UPDATE statements for changed fields on flush.
Question 7: Which annotation is used to embed a non-entity object as columns inside an entity's table?
- @Embedded / @Embeddable (Correct answer)
- @OneToOne with shared PK
- @SecondaryTable
- @ElementCollection
Correct answer: @Embedded / @Embeddable
@Embeddable marks a class whose fields are stored in the owning entity's table, and @Embedded is placed on the field in the entity.
What exception does Spring's `@Transactional` mechanism translate a `ConstraintViolationException` into?