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 |
Tags
- 네트워크 설정
- resttemplate
- 처음 만나는 AI수학 with Python
- network configuration
- 코드로배우는스프링부트웹프로젝트
- 알파회계
- 데비안
- baeldung
- 처음 만나는 AI 수학 with Python
- ㅒ
- 자바편
- d
- 이터레이터
- 스프링부트핵심가이드
- 친절한SQL튜닝
- 스프링 시큐리티
- 자료구조와함께배우는알고리즘입문
- 자료구조와 함께 배우는 알고리즘 입문
- 페이징
- 선형대수
- 서버설정
- 코드로배우는스프링웹프로젝트
- iterator
- GIT
- 목록처리
- 리눅스
- /etc/network/interfaces
- 구멍가게코딩단
- Kernighan의 C언어 프로그래밍
- 티스토리 쿠키 삭제
Archives
- Today
- Total
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"'.
'Framework > Spring' 카테고리의 다른 글
[bootBoard] N:1(다대일) 연관관계: 9-2. 프로젝트 적용: 게시물 목록 처리 (0) | 2022.10.05 |
---|---|
[bootBoard] N:1(다대일) 연관관계: 9-1. 프로젝트 적용: 게시물 등록 처리 (0) | 2022.10.04 |
[bootBoard] N:1(다대일) 연관관계: 8-1. 목록화면에 필요한 JPQL 생성 (0) | 2022.09.28 |
[bootBoard] N:1(다대일) 연관관계: 7-2. JPQL과 left(outer) join 연관관계 X (0) | 2022.09.28 |
[bootBoard] N:1(다대일) 연관관계: 7-1. JPQL과 left(outer) join 연관관계 O (0) | 2022.09.28 |
Comments