Repository Flashcards

1
Q

Repository

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

JpaRepository

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

From Service Class

A

@Service
public class UserService {

@Autowired
private UserRepository userRepository;

// Get all users
public List<User> getAllUsers() {
    return userRepository.findAll();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Custom Queries in JPA Repository - Method Query Derivation

A

Spring Data JPA can automatically generate SQL queries based on method names.
List<User> findByEmail(String email);</User>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Custom Queries in JPA Repository - Using @Query

A

@Query(“SELECT u FROM User u WHERE u.name = ?1”)
List<User> findUsersByName(String name);</User>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Custom Queries in JPA Repository - Using Native Queries

A

@Query(value = “SELECT * FROM users WHERE email = ?1”, nativeQuery = true)
List<User> findUsersByEmail(String email);</User>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Pagination and Sorting

A

JpaRepository extends PagingAndSortingRepository, allowing you to add pagination and sorting functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Transactions

A

By default, Spring manages transactions, ensuring that operations like save(), delete(), and findById() are executed in the scope of a transaction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Spring Data JPA Working

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Dependencies

A

Spring Boot Starter Data JPA
Database Driver (e.g., H2 or MySQL)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Configuring Database Connection

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Entity Class

A

The entity class represents a database table and is annotated with @Entity.
@Entity
public class User {
public interface UserRepository extends JpaRepository<User, Long> {

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Key Repository Interfaces

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly