Spring Data Jpa
[JPA] 데이터 중복 체크 existsBy vs countBy
DB에 Insert 하는 기능을 개발 중 이미 존재하는 데이터인지 중복 체크하는 로직을 추가해야 하는 상황이 있을 것이다... 예를 들어 회원가입 시 ID 중복 체크를 한다면 Spring Data Jpa의 쿼리메소드 중 countByUserId() 메소드를 사용해서 중복체크를 할 수 있지만, countByUserId()보다는 existsByUserId()를 써서 중복체크를 하도록 하자. public interface MemberRepository extends JpaRepository { // 모든 row를 순회한다. Long countByUserId(String userId); // 조건을 만족하는 데이터 존재 시 종료 boolean existsByUserId(String userId); } count..
2023. 12. 17. 00:26