Spring Framework Spring Data & JPA 1 — Questions and Answers
Question 1: Which annotation marks a Java class as a JPA entity that can be persisted in a relational database?
- @Component
- @Entity (Correct answer)
- @Repository
- @Persistent
Correct answer: @Entity
@Entity designates a Java class as a JPA entity, mapping it to a database table for persistence.
Question 2: Which Spring Data interface provides the minimum set of CRUD operations for a repository?
- JpaRepository
- Repository
- CrudRepository (Correct answer)
- PagingAndSortingRepository
Correct answer: CrudRepository
CrudRepository defines the core CRUD methods (save, findById, findAll, delete, count) that all Spring Data repositories can extend.
Question 3: What does the @GeneratedValue annotation configure when used alongside @Id in a JPA entity?
- The column name for the primary key
- The strategy used to generate primary key values (Correct answer)
- The default value assigned to a column
- The sequence ordering of entity records
Correct answer: The strategy used to generate primary key values
@GeneratedValue specifies the strategy (AUTO, IDENTITY, SEQUENCE, TABLE) used to automatically generate primary key values.
Question 4: Which annotation is used in a Spring Data JPA repository interface to define a custom JPQL or native SQL query?
- @CustomQuery
- @Query (Correct answer)
- @SQL
- @NamedQuery
Correct answer: @Query
@Query allows you to define a custom JPQL or native SQL query directly on a repository method using its value or nativeQuery attributes.
Question 5: What is the primary effect of applying @Transactional to a Spring service method?
- The method runs asynchronously on a separate thread
- The method executes within a database transaction, with automatic commit or rollback (Correct answer)
- The method's results are cached for subsequent calls
- The method is scheduled to run at a fixed interval
Correct answer: The method executes within a database transaction, with automatic commit or rollback
@Transactional wraps the method in a database transaction, automatically committing on success or rolling back on exception.
Question 6: How does saveAndFlush() in JpaRepository differ from save()?
- It immediately synchronizes the entity state with the database within the current transaction (Correct answer)
- It saves the entity only if it does not already exist in the database
- It saves the entity asynchronously without blocking
- It saves and then invalidates any cached version of the entity
Correct answer: It immediately synchronizes the entity state with the database within the current transaction
saveAndFlush() calls save() and then immediately flushes the persistence context, pushing changes to the database within the current transaction.
Question 7: What is the return type of the findById() method defined in Spring Data's CrudRepository?
- T
- List<T>
- Optional<T> (Correct answer)
- Collection<T>
Correct answer: Optional<T>
findById() returns Optional<T> to safely handle the case where no entity with the given ID exists, avoiding NullPointerExceptions.
Which annotation marks a Java class as a JPA entity that can be persisted in a relational database?