Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

['21,07,18] 중복제거 본문

Project/영화리뷰 관리

['21,07,18] 중복제거

Seung__ 2021. 7. 18. 11:08

그런데 또 다른 문제가 있다.

 

동일한 영화를 여러 번 저장이 가능하다. 

 

[
  {
    "id": 1,
    "title": "날씨의 아이",
    "message": null,
    "stateCode": null,
    "link": "https://movie.naver.com/movie/bi/mi/basic.nhn?code=181114",
    "image": "https://ssl.pstatic.net/imgmovie/mdi/mit110/1811/181114_P51_143716.jpg",
    "subtitle": "Weathering With You",
    "director": "신카이 마코토|",
    "actor": "심규혁|김유림|최한|강은애|이장원|손정아|다이고 코타로|모리 나나|오구리 슌|혼다 츠바사|히라이즈미 세이|",
    "userRating": "7.95",
    "commentDTOs": []
  },
  {
    "id": 2,
    "title": "날씨의 아이",
    "message": null,
    "stateCode": null,
    "link": "https://movie.naver.com/movie/bi/mi/basic.nhn?code=181114",
    "image": "https://ssl.pstatic.net/imgmovie/mdi/mit110/1811/181114_P51_143716.jpg",
    "subtitle": "Weathering With You",
    "director": "신카이 마코토|",
    "actor": "심규혁|김유림|최한|강은애|이장원|손정아|다이고 코타로|모리 나나|오구리 슌|혼다 츠바사|히라이즈미 세이|",
    "userRating": "7.95",
    "commentDTOs": []
  },
  {
    "id": 3,
    "title": "날씨의 아이",
    "message": null,
    "stateCode": null,
    "link": "https://movie.naver.com/movie/bi/mi/basic.nhn?code=181114",
    "image": "https://ssl.pstatic.net/imgmovie/mdi/mit110/1811/181114_P51_143716.jpg",
    "subtitle": "Weathering With You",
    "director": "신카이 마코토|",
    "actor": "심규혁|김유림|최한|강은애|이장원|손정아|다이고 코타로|모리 나나|오구리 슌|혼다 츠바사|히라이즈미 세이|",
    "userRating": "7.95",
    "commentDTOs": []
  },
  {
    "id": 4,
    "title": "날씨의 아이",

 

그 다음 제목으로 DB에서 영화 검색을 하면 에러가 발생한다.

 

 

해결 방법은 두 가지가 있을 것 같다.

 

1) 처음부터 영화가 중복되어 저장되지 않거나,

 

2) 중복되어 저장되더라도, 검색시 오류가 발생하지 않도록 list로 불러와서 첫 번째 결과만 보여주거나.

 

하지만 2번은 근본적인 해결 방법이 아니니, DB에 저장할 때 부터 중복 확인을 추가해보자.

 

1. MovieRepository


movieRepository에 주어진 조건으로 entity가 DB에 존재하는지 check하는 method를 선언해주자.

 Boolean existsByTitle(String title);

 

해당 method를 통해 입력된 제목과 일치하는 data가 있으면 true를 return 한다.

 

//add to list
    public MovieDTO addToMovieList(MovieDTO movieDTO){

        //DB에 해당 영화가 존재하는지 확인
        if (movieRepository.existsByTitle(movieDTO.getTitle())) {

            log.info("중복저장 불가");
            movieDTO.setStateCode(StateCode.SC_OVERLAPPED);

            return movieDTO;

        } else {

            var movieEntity = movieDTOToEntity(movieDTO);

            movieRepository.saveAndFlush(movieEntity);

            movieDTO.setStateCode(StateCode.SC_OK);

            return movieDTO;

        }


    }

 

2. 중복저장 시도


1) 첫 번째 저장

2) 두 번째 저장 : 첫 번째 data와 중복되어 저장이 불가능함.

2021-07-18 10:47:18.294  INFO 34556 --- [nio-8080-exec-2] c.e.m.m.service.
MovieListService         : 중복저장 불가

 

3) findAll : 중복저장 되지 않아 1개만 존재한다.

 

4) findByTitle : 이 전에는 중복저장이 가능하여, title로 검색 시 여러 결과가 존재할 경우 에러가 발생했다.

 

 

정상적으로 저장이 되었다.

 

 

3. GitHub : 210718, data overlap check


 

bsh6463/MovieManager

Contribute to bsh6463/MovieManager development by creating an account on GitHub.

github.com

 

Comments