관리 메뉴

bright jazz music

[bootBoard] N:1(다대일) 연관관계: 8-2. 조회 화면에 필요한 JPQL 생성 본문

Framework/Spring

[bootBoard] N:1(다대일) 연관관계: 8-2. 조회 화면에 필요한 JPQL 생성

bright jazz music 2022. 9. 29. 23:32
  • 조회 화면에서는 Board와 Member를 주로 이용한다.
  • 해당 게시물이 몇 개의 댓글이 있는지 알려주는 수준까지 작성한다.
  • 실제 댓글은 화면에서 주로 AJAX를 이용해 필요한 순간에 동적으로 데이터를 가져온느 방식이 일반적이다.

 

조회 화면을 위해 작성할 JPQL은 아래와 같다.

 

1. BoardRepository 인터페이스에 JPQL 코드 추가

// BoardRepository.java

public interface BoardRepository extends JpaRepository<Board, Long> {
...

	@Query("SELECT b, w, count(r) " +
    	    " FROM Board b LEFT JOIN b.writer w " +
        	" LEFT OUTER JOIN Reply r ON r.board = b " +
        	" WHERE b.bno = :bno")

	Object getBoardByBno(@Param("bno") Long bno);

...

}

BoardRepository에 추가된 getBoardByBno()는 목록처리와 유사하지만 특정 게시물 번호를 사용하는 부분이 다르다.

 

2. BoardRepositoryTests.java 클래스에 테스트 코드 작성

//BoardRepositoryTests.java
...
	@Test
	public void testRead3() {
    	Object result = boardRepository.getBoardByBno(100L);
    	Object[] arr = (Object[]) result;
    	System.out.println(Arrays.toString(arr));
	}
...

 

 

콘솔 결과

 

2022-09-29 23:30:58.830  INFO 8444 --- [    Test worker] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-09-29 23:30:59.056  WARN 8444 --- [    Test worker] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-09-29 23:31:00.251  INFO 8444 --- [    Test worker] c.e.b.repository.BoardRepositoryTests    : Started BoardRepositoryTests in 4.155 seconds (JVM running for 5.893)
Hibernate: 
    select
        board0_.bno as col_0_0_,
        member1_.email as col_1_0_,
        count(reply2_.rno) as col_2_0_,
        board0_.bno as bno1_0_0_,
        member1_.email as email1_1_1_,
        board0_.moddate as moddate2_0_0_,
        board0_.reg_date as reg_date3_0_0_,
        board0_.content as content4_0_0_,
        board0_.title as title5_0_0_,
        board0_.writer_email as writer_e6_0_0_,
        member1_.moddate as moddate2_1_1_,
        member1_.reg_date as reg_date3_1_1_,
        member1_.name as name4_1_1_,
        member1_.password as password5_1_1_ 
    from
        board board0_ 
    left outer join
        member member1_ 
            on board0_.writer_email=member1_.email 
    left outer join
        reply reply2_ 
            on (
                reply2_.board_bno=board0_.bno
            ) 
    where
        board0_.bno=?
[Board(bno=100, title=Title...100, content=Content...100), Member(email=user100@aaa.com, password=11111, name=USER100), 4]
2022-09-29 23:31:00.649  INFO 8444 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2022-09-29 23:31:00.649  INFO 8444 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-09-29 23:31:00.666  INFO 8444 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
BUILD SUCCESSFUL in 17s
4 actionable tasks: 3 executed, 1 up-to-date
오후 11:31:01: Task execution finished ':test --tests "com.example.bootboard.repository.BoardRepositoryTests.testRead3"'.
Comments