2024.02.03 - [끄적이기] - JPA에 대해서
JPA에 대해서
JPA? 자바 퍼시스턴스(Java Persistence, 이전 이름: 자바 퍼시스턴스 API/Java Persistence API) 또는 자바 지속성 API(Java Persistence API, JPA)는 자바 플랫폼 SE와 자바 플랫폼 EE를 사용하는 응용프로그램에서 관
kwaksh2319.tistory.com
Spring Data 저장소 추상화의 중앙 인터페이스는 Repository입니다. 관리할 도메인 클래스와 도메인 클래스의 식별자 유형을 유형 인수로 사용합니다. 이 인터페이스는 주로 작업할 유형을 캡처하고 이 인터페이스를 확장하는 인터페이스를 검색하는 데 도움이 되는 마커 인터페이스 역할을 합니다. CrudRepository 및 ListCrudRepository 인터페이스는 관리되는 엔터티 클래스에 대한 정교한 CRUD 기능을 제공합니다.
public interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S entity); //1
Optional<T> findById(ID primaryKey); ///2
Iterable<T> findAll(); //3
long count(); //4
void delete(T entity); //5
boolean existsById(ID primaryKey); //6
// … more functionality omitted.
}
- entity를 저장합니다.
- entity에서 id 식별자를 통해서 return 합니다.
- 모든 entity를 return 합니다.
- entities들의 숫자를 return합니다.
- entity를 삭제합니다.
- id 식별자를 entity에서 존재 여부를 확인합니다.
이 메서드들은 CRUD 메서드들를 참조하여 일반적으로 인터페이스에 명시된 메서들입니다.
ListCrudRepository는 동등한 메서들을 제공합니다. 그러나 CrudRepository는 Iterable리턴하지만
ListCrudRepository는 list를 return 합니다.
CrudRepository 외에도 엔터티에 대한 페이지 매김 액세스를 쉽게 하기 위해 추가 메서드를 추가하는 PagingAndSortingRepository 및 ListPagingAndSortingRepository가 있습니다.
public interface PagingAndSortingRepository<T, ID> {
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}
페이지 크기 20으로 User의 두 번째 페이지에 액세스하려면 다음과 같이 할 수 있습니다.
PagingAndSortingRepository<User, Long> repository = // … get access to a bean
Page<User> users = repository.findAll(PageRequest.of(1, 20));
ListPagingAndSortingRepository는 동등한 메서드를 제공하지만 PagingAndSortingRepository 메서드가 Iterable을 반환하는 List를 반환합니다
쿼리 방식 외에도 개수 쿼리와 삭제 쿼리 모두에 대한 쿼리 파생이 가능합니다. 다음 목록은 파생된 카운트 쿼리에 대한 인터페이스 정의를 보여줍니다.
interface UserRepository extends CrudRepository<User, Long> {
long countByLastname(String lastname);
}
다음 목록은 파생된 삭제 쿼리에 대한 인터페이스 정의를 보여줍니다.
interface UserRepository extends CrudRepository<User, Long> {
long deleteByLastname(String lastname);
List<User> removeByLastname(String lastname);
}
https://docs.spring.io/spring-data/jpa/reference/repositories/core-concepts.html
Core concepts :: Spring Data JPA
The methods declared in this interface are commonly referred to as CRUD methods. ListCrudRepository offers equivalent methods, but they return List where the CrudRepository methods return an Iterable.
docs.spring.io
'웹 > Spring' 카테고리의 다른 글
Spring Data JPA - 구성 (0) | 2024.02.25 |
---|---|
Spring Data JPA- repository 인터페이스의 정의 (0) | 2024.02.18 |
Spring Data JPA에 대해서 (0) | 2024.02.03 |
톰캣의 war 실행되는 원리? (0) | 2024.01.14 |
Gradle에서 compileOnly와 implementation 차이 (0) | 2024.01.09 |