Repository Flashcards
Repository
In JPA, a Repository is an interface or class that provides CRUD (Create, Read, Update, Delete) operations for managing entities in a database. It abstracts the data access layer, allowing you to work with entities without needing to write raw SQL or JPQL queries
JpaRepository
A special interface provided by Spring Data JPA, offering methods for common operations like saving, deleting, finding by ID, and pagination.
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
// You can define custom queries here if needed
}
JpaRepository<User, Long> means that this repository works with the User entity, and the ID type of the User entity is Long.
The @Repository annotation indicates that this is a Spring Data repository. It also ensures that any database exceptions are translated into Spring’s DataAccessException class.
From Service Class
@Service
public class UserService {
@Autowired private UserRepository userRepository; // Get all users public List<User> getAllUsers() { return userRepository.findAll(); }
Custom Queries in JPA Repository - Method Query Derivation
Spring Data JPA can automatically generate SQL queries based on method names.
List<User> findByEmail(String email);</User>
Custom Queries in JPA Repository - Using @Query
@Query(“SELECT u FROM User u WHERE u.name = ?1”)
List<User> findUsersByName(String name);</User>
Custom Queries in JPA Repository - Using Native Queries
@Query(value = “SELECT * FROM users WHERE email = ?1”, nativeQuery = true)
List<User> findUsersByEmail(String email);</User>
Pagination and Sorting
JpaRepository extends PagingAndSortingRepository, allowing you to add pagination and sorting functionality.
Transactions
By default, Spring manages transactions, ensuring that operations like save(), delete(), and findById() are executed in the scope of a transaction.
Spring Data JPA Working
Spring Data JPA simplifies interaction with databases by providing a repository layer that performs CRUD operations and query execution without needing to manually write SQL.
It automatically generates the implementation of the repository interfaces at runtime based on your method names. For example, methods like findByName(), deleteById() are automatically implemented by Spring Data JPA.
Spring Data JPA integrates with Spring’s transaction management, so operations are automatically wrapped in transactions. For example, if you’re saving or deleting entities, Spring ensures these operations are executed within a transactional context.
When used with Spring Boot, Spring Data JPA provides auto-configuration that simplifies the setup. With Spring Boot, you don’t need to manually configure the EntityManagerFactory or DataSource; Spring Boot handles that for you.
Dependencies
Spring Boot Starter Data JPA
Database Driver (e.g., H2 or MySQL)
Configuring Database Connection
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
Entity Class
The entity class represents a database table and is annotated with @Entity.
@Entity
public class User {
public interface UserRepository extends JpaRepository<User, Long> {
Key Repository Interfaces
Repository<T, ID> - This is the most general-purpose interface and is the root of the repository hierarchy in Spring Data JPA
JpaRepository<T, ID> - This is the core interface you typically use for JPA-based repositories.
It provides various methods like findAll(), findById(), save(), deleteById(), and pagination-related methods.
CrudRepository<T, ID> - This interface extends Repository and provides basic CRUD operations (save(), findOne(), delete(), etc.).
PagingAndSortingRepository<T, ID> - This extends CrudRepository and provides additional methods for pagination and sorting.