Spring Data and Persistence Flashcards
What is Spring Data JPA?
A part of Spring Data that simplifies working with JPA-based repositories by providing common CRUD operations and query methods.
What is the @Entity annotation in JPA?
It marks a class as a JPA entity mapped to a database table.
What is the role of @Repository in Spring Data?
It is used to indicate that the class provides CRUD or data manipulation operations on a database.
What is the @Transactional annotation used for?
To manage transactions declaratively in Spring.
How can you create a repository interface in Spring Data JPA?
By extending JpaRepository or CrudRepository.
How does Spring Data JPA handle pagination?
Using the Pageable interface and returning Page<T> results from repository methods.</T>
What is Lazy and Eager loading in JPA?
Lazy loading fetches data on demand, while Eager loading fetches all related entities upfront.
How can you define a custom query in Spring Data JPA?
Using the @Query annotation in repository interfaces.
What is the difference between save() and saveAndFlush() in Spring Data JPA?
save() saves an entity but may not flush it to the database immediately, while saveAndFlush() forces an immediate flush.
What is the purpose of the @Id annotation in JPA?
It marks a field as the primary key for the entity.