
public interface UserRepository extends JpaRepository<User, Long> {
// User : JPA entity, Long : Primary Key type
public User findByNickname(String nickname)
}
Spring Data JPA Magic
- All above XXXRepository are Interfaces
- By extending those, you inherit all the functions defined under those (CRUD operations, sorting, etc)
- You can use these functions without implementing the methods. How?
- When Spring application is launched, Spring Data JPA creates a proxy object which implements the custom interface.
- This proxy object intercepts method calls made to the interface's functions and analyzes the method name and parameters to determine what it will do
- Then it will dynamically generate the appropriate database query
- JPA provider (ex, Hiberante) will execute the dynamically generated query on the underlying database
Spring Data JPA Naming Conventions
| Method | Description |
| save() | Save a record |
| delete() | Deletes a record |
| count() | Counts the number of records |
| findOne() | Find one record |
| findAll() | Find all records |
| Keyword | Example |
| And | findByNameAndEmail(String name, String email) |
| GreaterThan | findByAgeGreaterThan(int age) |
| IsNull | findByNameIsNull() |
Can find more from https://docs.spring.io/spring-data/jpa/docs/1.10.1.RELEASE/reference/html/
'Spring' 카테고리의 다른 글
| Spring Boot Exception Handling (0) | 2023.09.14 |
|---|---|
| DTO (Data Transfer Object) (0) | 2023.09.14 |
| Spring Data JPA Entity (0) | 2023.09.01 |
| Spring vs Spring Boot (0) | 2023.08.30 |
| JPA and ORM (0) | 2023.08.30 |