Spring Framework Spring Data & JPA 2 — Questions and Answers
Question 1: What Spring Data JPA feature automatically generates a query based on a repository method's name, such as findByLastNameAndAge?
- JPQL query parsing
- Derived query methods (Correct answer)
- Named query resolution
- Criteria API generation
Correct answer: Derived query methods
Spring Data JPA's derived query methods parse method names to automatically generate JPQL queries based on keywords like findBy, And, Or, and property names.
Question 2: Which JPA annotation establishes a one-to-many relationship on the owning entity side?
- @ManyToOne
- @OneToOne
- @OneToMany (Correct answer)
- @ManyToMany
Correct answer: @OneToMany
@OneToMany defines the relationship where one entity instance is associated with multiple instances of another entity.
Question 3: What additional capabilities does JpaRepository provide beyond those in PagingAndSortingRepository?
- Support for derived query methods
- JPA-specific operations like flush(), saveAllAndFlush(), and deleteAllInBatch() (Correct answer)
- Built-in caching and second-level cache support
- Automatic transaction management for all methods
Correct answer: JPA-specific operations like flush(), saveAllAndFlush(), and deleteAllInBatch()
JpaRepository extends PagingAndSortingRepository and adds JPA-specific methods such as flush(), saveAllAndFlush(), deleteAllInBatch(), and getReferenceById().
Question 4: What is the default FetchType for a @ManyToOne relationship in JPA?
- LAZY
- EAGER (Correct answer)
- NONE
- AUTO
Correct answer: EAGER
@ManyToOne defaults to FetchType.EAGER, meaning the related entity is loaded immediately when the owning entity is fetched.
Question 5: What is the primary purpose of Spring Data JPA Specifications?
- To declare entity table constraints and column definitions
- To build dynamic, composable query predicates using the JPA Criteria API (Correct answer)
- To configure the data source and connection pool settings
- To define transaction propagation rules for repository methods
Correct answer: To build dynamic, composable query predicates using the JPA Criteria API
Specifications implement the Specification<T> interface to create reusable, composable query predicates built on the JPA Criteria API.
Question 6: Which interface is passed as a method parameter to a Spring Data repository method to support pagination?
- PageInfo
- Pageable (Correct answer)
- Pagination
- PageSlice
Correct answer: Pageable
Pageable is the Spring Data interface that encapsulates page number, page size, and sorting information to enable paginated repository queries.
Question 7: What does the @Column annotation allow you to customize on a JPA entity field?
- The Java data type and nullability of the field in the application layer
- Database column properties such as name, length, nullable, and unique constraints (Correct answer)
- The relationship between the field and a foreign key in another table
- The caching strategy and eviction policy for the field's value
Correct answer: Database column properties such as name, length, nullable, and unique constraints
@Column maps a field to a specific database column and lets you configure the column's name, length, nullable, and uniqueness constraints.
What Spring Data JPA feature automatically generates a query based on a repository method's name, such as findByLastNameAndAge?