Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스
- 쓰레드 #쓰레드풀 #프로세스
- 챌린저스 #bod
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #AI #서버 #자동화 #SQL #KDT #기본문법 #데이터베이스 #Computer #Science #CPU #메모리
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #대용량트래픽 #처리방법 #캐싱 #코드최적화 #db최적화 #트래픽 #로드밸런서
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #aop #관점지향프로그래밍 #유지보수
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스 #웹개발
- 스파르타 코딩클럽 #인메모리db #h2 #연동이슈 #문제해결 #방법 #spring security #header #
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #ci #cd
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #딥러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #딥러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스 #DBMS #Oracle #MongoDB #아키텍쳐 #DB
- 스파르타 코딩클럽 #내배캠 #최종프로젝트 #4개월삭제 #국비지원 #자바
- 스파르타 코딩클럽 #내배캠 #최종프로젝트 #로그인 #인증인가 #jwt? #토큰인증 #액세스토큰 #리프레시토큰 #쿠키 #파싱 #서명키의 중요성 #security context holder
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #springmvc패턴 #model #view #controller
- ci/cd파이프라인
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #소셜로그인 #네이버 #기능구현 #vue.js #spring boot #네이버로그인 #연동하기
- 보안 #이슈
- java5기
- 인가 #
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #get방식 #post방식 #http프로토콜 #클라이언트 #백엔드 #api
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #OSI #ISO #AI #서버 #자동화 #SQL #기본문법 #데이터베이스 #DBMS #Oracle #MongoDB #아키텍쳐 #DB
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #ci #ioc #의존성
- 프로세스 #CPU #시공유 #커널
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #tdd #테스트 주도개발 #테스트코드 #유닛테스트
- spring security #jwt 토큰 #json web token #token #직렬화 #인증
- 공간복잡도 #공간자원 #캐시메모리 #SRAM #DRAM #시간복잡도
- 비트 #바이트 #이진수
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #트랜잭션전파 #transaction
- 스프링 #백엔드 #자바
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #최종프로젝트 #aws s3 #프로필 이미지 수정 #자동삭제
Archives
- Today
- Total
요리사에서 IT개발자로
SQL 쿼리 delete메소드 작동에 대해 본문
void deleteTop1ByUserIdOrderByCreatedAtAsc(Long userId);
DELETE FROM your_table
WHERE id = (
SELECT id FROM your_table
WHERE user_id = ?
ORDER BY created_at DESC
LIMIT 1
);
위에 주어진 SQL 쿼리 메소드는
주어진 사용자에 대해
생성 날짜 기준으로 오름차순으로 정렬된 상위레코드를 삭제하는 쿼리메소드 이다.
// 가장 오래된 비밀번호 기록 삭제
if (userPasswordRecordList.size() >= 3) {
userPasswordRepository.deleteByUserIdOrderByCreatedAtAsc(userDetails.getUserId());
}
그러나 일부 데이터베이스에서
Delete 문 내에 서브쿼리를 직접 실행 하는 것이
불가능 할 수 있다.
해결하는 방법
먼저 삭제할 객체를 찾아온 후
삭제 명령 delete()안의 넣어주는 방법을 이용하여
// 가장 오래된 비밀번호 기록 삭제
if (userPasswordRecordList.size() >= 3) {
UserPasswordRecord oldPassword = userPasswordRepository.findByUserIdAndCreatedAt(userDetails.getUserId(),
userPasswordRecordList.get(2).getCreatedAt());
user.removePasswordRecord(oldPassword);
}
위와 같은 구문으로 직접 찾아와서
oldPassword로 해당 객체를 전달해준 뒤에
직접 삭제하는것이
더욱 직관적이며 코드자체로도 명시가 되어있기에
delete 쿼리로 실행하는것보다 안전하다고 볼 수 있다.
아직 배우지는 않았지만
@Query를 사용하여 직접 하드코딩 하므로 삭제가 가능하게 가능도 하다고 한다.
@Query(value = "DELETE FROM my_entity WHERE id = (" +
"SELECT id FROM my_entity ORDER BY created_at DESC LIMIT 1 OFFSET 2" +
")", nativeQuery = true)
void deleteOldestIfMoreThanThree();
반응형
'Spring' 카테고리의 다른 글
스파르타 코딩클럽 JPA 심화 JPARepository 기능 (0) | 2024.06.27 |
---|---|
스파르타 코딩클럽 JPA 심화 트랜잭션 (0) | 2024.06.26 |
PasswordEncoder 한 비밀번호를 비교해주는 matches와 이유 (0) | 2024.06.21 |
스파르타 부트캠프 JPA문제(다락방) (0) | 2024.06.19 |
스파르타 부트캠프 Query Method (다락방) (0) | 2024.06.18 |