Spring Boot Data Persistence with JPA 3 — Questions and Answers
Question 1: Which interface should a Spring Data JPA repository extend to get CRUD operations plus pagination support?
- CrudRepository
- JpaRepository (Correct answer)
- PagingAndSortingRepository
- SimpleJpaRepository
Correct answer: JpaRepository
JpaRepository extends PagingAndSortingRepository and CrudRepository, providing the full set including pagination, sorting, and batch operations.
Question 2: What does the `@Transient` annotation do on an entity field?
- Marks the field for lazy loading
- Excludes the field from database persistence (Correct answer)
- Creates a transient index on the column
- Enables caching for that field
Correct answer: Excludes the field from database persistence
@Transient tells JPA to skip the field during persistence, so it is never mapped to a database column.
Question 3: In Spring Boot, which property sets the Hibernate DDL generation strategy to create tables if they don't exist?
- spring.jpa.hibernate.ddl-auto=create
- spring.jpa.hibernate.ddl-auto=update (Correct answer)
- spring.jpa.hibernate.ddl-auto=validate
- spring.jpa.hibernate.ddl-auto=create-drop
Correct answer: spring.jpa.hibernate.ddl-auto=update
The `update` strategy creates missing tables and columns but does not drop existing ones, making it safe for existing data.
Question 4: What does the `@ManyToMany` annotation require to store the relationship in the database?
- A foreign key on one of the entity tables
- A join table mapping the two entity PKs (Correct answer)
- A composite primary key on each entity
- An @EmbeddedId on the owning side
Correct answer: A join table mapping the two entity PKs
@ManyToMany relationships are stored in a join (junction) table that holds the primary keys of both associated entities.
Question 5: Which method on `JpaRepository` fetches an entity and throws `EmptyResultDataAccessException` if not found?
- findById() (Correct answer)
- getOne()
- getReferenceById()
- findOne()
Correct answer: findById()
findById() returns an Optional and does not throw, but the Spring Data `getById` / `getReferenceById` returns a proxy; for eager throwing behavior you typically use findById() + orElseThrow().
Question 6: What is the role of `EntityManager.flush()` in JPA?
- Clears the first-level cache
- Synchronizes the persistence context with the database (Correct answer)
- Commits the current transaction
- Reloads entities from the database
Correct answer: Synchronizes the persistence context with the database
flush() forces JPA to execute pending SQL statements against the database without committing the transaction.
Question 7: Which annotation lets you define a named JPQL query directly on an entity class?
- @Query
- @NamedQuery (Correct answer)
- @NativeQuery
- @JpqlQuery
Correct answer: @NamedQuery
@NamedQuery is placed on the entity class and associates a JPQL string with a name that can be referenced at runtime.
Which interface should a Spring Data JPA repository extend to get CRUD operations plus pagination support?