오늘 한 일

  • 연습 프로젝트 README.md 추가

연습 프로젝트에 H2 - JPA로 연결하기

  • build.gradle에 h2, jpa 추가
  • application.properties에 h2, jpa 관련 설정 추가
  • domain에 @Entity 붙이고 JPA entity 설정해주기
  • JPARepository 클래스 생성해 repo 인터페이스 구현
  • service 파일에 @transactional 추가
  • src 루트 폴더에 SpringConfig.java 파일 생성 후 @Configuration 어노테이션 작성
  • springConfig 파일에서 repo 바꾸고 빈 등록
  • h2 데이터베이스에 테이블 생성 (생각해보니 jpa가 만들어줄수도 있다고 했는데)

dataSource 에러 해결

  • 동료분 dataSource 연결 에러 발생

    2022-08-18 10:43:58.788 ERROR 8108 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: java.lang.IllegalArgumentException: No DataSource specified] with root cause
    java.lang.IllegalArgumentException: No DataSource specified ...
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:103) ~[spring-jdbc-5.3.22.jar:5.3.22]
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-5.3.22.jar:5.3.22]
    at hello.hellospring.repository.JdbcMemberRepository.getConnection(JdbcMemberRepository.java:122) ~[main/:na]
    at hello.hellospring.repository.JdbcMemberRepository.findAll(JdbcMemberRepository.java:78) ~[main/:na]
    at hello.hellospring.service.MemberService.findMember(MemberService.java:46) ~[main/:na]
    at hello.hellospring.controller.MemberController.list(MemberController.java:46) ~[main/:na]

  • SpringConfig에서 dataSource를 주입해주어야 하는데 이 부분에서 오류가 나면 아래와 같이 DataSourceUtils, repository, service, controller 줄줄이 에러가 난다고 뜬다.

  • 인프런에도 비슷한 에러가 있었는데 그 분은 dataSource 변수명을 DataSource로 잘못 써서 연결이 안 된거였다.

  • 동료분은 dataSource 주입하는 생성자를 통째로 빠뜨림...!

junit test - Assertion 에러 해결

비슷한 인프런 질문

org.opentest4j.AssertionFailedError: Expected java.lang.IllegalStateException to be thrown, but nothing was thrown

  • 중복이름 예외가 발생해야 하는데 뜨지 않음
  • user2.setName()을 빠뜨림
// test.java
@Test
void 중복회원예외 () {
	// given
	User user = new User();
	user.setName("aaa");
	
	User user2 = new User();
	user2.setName("aaa"); //user.setName("aaa"); 이래서 에러남
	
	// when
	userService.join(user);		
	
	// then
	IllegalStateException e = assertThrows(IllegalStateException.class, () -> userService.join(user2));
	Assertions.assertThat(e.getMessage()).isEqualTo("이미 존재하는 이름입니다.");
}

JPQL 쿼리

JPA-delete 쿼리 참고

  • 나는 jdbcTemplate은 건너뛰고 바로 jpa (jpql 작성)으로 넘어갔음
  • 강의에는 생성, 조회밖에 없는데 나는 수정, 삭제도 넣기로 해서 jqpl 작성법을 찾아보았다.
  • 솔직히 조회, 생성과 다르게 merge()executeUpdate()를 사용해야 하는 이유는 이해 못했지만 내가 원하는 대로 작동은 잘 된다!
// JpaPostRepository.java
@Override
public Post update(Post post) {
	post.setUpdatedAt(new Date());
	em.merge(post);
	return post;
}

@Override
public void delete(Long postId) {
	em.createQuery("delete from Post p where p.postId = :postId")
			.setParameter("postId", postId)
			.executeUpdate();
}

용어 정리

  • BNPL (Buy Now Pay Later) : 온라인 할부금융 업체. 온라인에서 저렴하고 쉽게 제공
profile
문서화를 좋아하는 개발자

0개의 댓글