본문 바로가기

JPA9

[JPA] Error creating bean with name 'entityManagerFactory' defined in class path resource 에러 해결 spring 버전 : 2.2.4 gradle 버전 : 6.4.1 //postgresql 의존성 추가 compile group: 'org.postgresql', name: 'postgresql' JPA + Spring Boot + H2를 사용하는 프로젝트에서 PostgreSql를 연동하려고 접속 정보를 입력 후 메인을 실행시켰는데 아래와 같은 에러가 발생했다. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/Hiberna.. 2020. 8. 5.
[JPA] update가 왜 안되는거지..? (detached to persistent) 문제의 메소드 @PostMapping("/settings/profile") public String updateProfile(@CurrentUser Account account, @Valid Profile profile, Errors errors, Model model){ if(errors.hasErrors()){ model.addAttribute(account); return "/settings/profile"; } //데이터 변경 작업은 서비스 쪽에 위임 accountService.updateProfile(account, profile); //사용자의 form submit이 다시 일어나지 않도록 redirect return "redirect:/" + "settings/profile"; } 분명히 up.. 2020. 7. 21.
[JPA] Repository 인터페이스 작성시 유의사항 JpaRepository를 상속 받은 인터페이스를 작성 할 때, 기본 제공 메소드만 쓰는 경우는 별로 없다고 생각한다. 필요로하는 커스텀 메소드가 필요하다. 이때 간과해서는 안될 아주 중요한 부분이 있다..!!! 기본적으로 JpaRepository 인터페이스에는 @Transactional 이 달려있다. 따라서 해당 메소드를 거친 엔티티는 계속 영속성 상태이다. 하지만, 우리가 임의로 정의해준 메소드에는 @Transactional이 적용되지않는다. 따라서 정의한 메소드에도 Transactional이 정의되도록 꼭 어노테이션을 붙여줘야한다. 그렇지 않았다가는 나중에 어마어마한 시한폭탄이 될것이다.. //select 만 하는 경우 readOnly 옵션을 줌으로써 조금이나마 성능향상을 꾀할 수 있음 @Trans.. 2020. 7. 21.
[JPA] 이메일 인증 토큰 값이 Null ?? (EntityLifeCycle, Transactional) 백기선님의 JPA 웹앱 만들기 강의를 참고하였습니다. 회원가입시 이메일로 본인 확인을 검증하는 과정에서 임시로 발급한 token값이 null이어서 에러 페이지를 뱉어냈다. java.lang.NullPointerException: null at com.jpa.studywebapp.account.AccountController.checkEmailToken(AccountController.java:65) ~[main/:na] 왜그럴까? 여기서 한 번 더 JPA 라이프사이클의 중요성 을 깨닫는다. 문제의 소스코드 public void processNewAccount(SignUpForm signUpForm) { Account newAccount = saveNewAccount(signUpForm); newAccou.. 2020. 7. 14.
[JPA] spring data jpa Page vs Slice 페이지와 슬라이스는 무슨 차이점이 있을까?? Page findByLastname(String lastname, Pageable pageable); Slice findByLastname(String lastname, Pageable pageable); List findByLastname(String lastname, Sort sort); List findByLastname(String lastname, Pageable pageable); Pageable 객체를 쿼리 메소드로 전달해 쿼리에 페이징을 동적으로 추가할 수 있다. page는 사용 가능한 데이터의 총 개수 및 전체 페이지 수를 알 수 있다. 총 개수를 알아내기 위해 추가적으로 카운트 쿼리가 실행된다. 기본적으로 카운트 쿼리는 실제로 실행되는 쿼리에.. 2020. 6. 29.
[JPA] 변경 감지(dirty checking)와 병합(merge) 본 내용은 인프런 김영한님의 JPA 활용 강의를 수강하며 정리한 내용입니다. 준영속 엔티티란? 영속성 컨텍스트가 더는 관리하지 않는 엔티티를 말한다. 객체가 이미 DB에 한번 저장되어 식별자가 존재하는 경우, 기존 식별자를 가지고 있으면 준영속 엔티티로 볼 수 있다. jpa가 관리를 하지 않음, persist 상태가 아니기 때문에 값을 바꿔도 DB변경이 일어나지 않음 준영속 엔티티를 수정하는 2가지 방법 dirtyChecking을 사용하여 값을 저장 @Transactional void update(Item itemParam) { //itemParam: 준영속 상태의 엔티티 Item findItem = em.find(Item.class, itemParam.getId()); //같은 엔티티를 조회 findI.. 2020. 5. 2.