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
- json
- JoinColumn
- 인텔리제이
- queryDSL
- JPA
- spring서버
- 최종 프로젝트
- Unsatisfied dependency
- 1차캐시
- jpa에러
- JPA주의사항
- REST API 규칙
- github
- 스프링부트오류
- @IdClass
- Spring Spring boot 차이
- Filter
- Q 클래스
- REST란
- 빈생성안됨
- Error creating bean with name
- jwt메서드
- 스프링 부트 기능
- jpa회원가입
- uncheck Exception
- 스프링 부트 공식 문서
- 복합키
- json gson 차이
- ERD 작성
- git
Archives
- Today
- Total
Everyday Dev System
JPA) N : 1 관계 맵핑 (thread : Channel) 본문
스레드가 채널 안에 여러개가 있으므로, 다대1 양방향 관계입니다.
아래와 같이 Entity 클래스 코드를 작성할 수 있습니다.
1. Channel 엔티티 클래스 코드
public void addThread(Thread thread){
this.threadList.add(thread);
}
2. Thread 엔티티 클래스 코드
public void setChannel(Channel channel){
this.channel = channel;
channel.addThread(this);
}
3. Test Code
Channel , Thread 엔티티 객체를 생성자를 통해 만든 후에,
양방향 연관 관계 설정을 위해서 Thread 클래스 내에 연관 관계 편의 메서드인 setChannel()을 호출.
( 해당 setChannel() 메서드 내에서 Channel 클래스 내에 addThread() 메서드 호출 → 양방향 이므로.)
// given
var newChannel = Channel.builder().name("new-group").build();
var newThread = Thread.builder().message("new message").build();
newThread.setChannel(newChannel); // 채널과 스레드의 연관 관계 설정.
// setChannel 메서드 내부에는 양방향으로 관계를 맺도록 하였음.
4. 전체 Code
package me.chaeyoung.jpa.thread;
import me.chaeyoung.jpa.channel.Channel;
import me.chaeyoung.jpa.channel.ChannelRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@Transactional
@Rollback(value=false)
class ThreadRepositoryTest {
@Autowired
private ThreadRepository threadRepository;
@Autowired
private ChannelRepository channelRepository;
@Test
void insertSelectThreadTest(){
// given
var newChannel = Channel.builder().name("new-group").build();
var newThread = Thread.builder().message("new message").build();
var newThread2 = Thread.builder().message("new message2").build();
newThread.setChannel(newChannel); // 채널과 스레드의 연관 관계 설정.
newThread2.setChannel(newChannel);
// setChannel 메서드 내부에는 양방향으로 관계를 맺도록 하였음.
// when
var savedChannel = channelRepository.insertChannel(newChannel);
var savedThread = threadRepository.insertThread(newThread);
var savedThread2 = threadRepository.insertThread(newThread2);
// then
var foundChannel = channelRepository.selectChannel(savedChannel.getId());
assert foundChannel.getThreadList().containsAll(Set.of(savedThread, savedThread2));
}
}
아래 코드에 break point를 걸고, 디버깅을 하면 위와 같이 불러옴을 알 수 있습니다.
assert foundChannel.getThreadList().containsAll(Set.of(savedThread, savedThread2));
getThreadList는 LinkedHashSet 타입을 반환하는 메서드.
containsAll은 해당 메서드 안에 매개변수 Collection<?> c 의 존재 여부에 따라 boolean 값을 반환합니다.
'내배캠 주요 학습 > JPA 심화' 카테고리의 다른 글
JPA 복합키 설정하기 - @IdClass (0) | 2023.07.31 |
---|---|
JPA) N : 1 관계 맵핑 (Thread : Channel : User) (0) | 2023.07.31 |
Spring Framework와 JPA 활용 (0) | 2023.07.31 |
Entity Class에 활용 어노테이션 - 1주차 4강 (0) | 2023.07.30 |
자바 어플리케이션에서 도커 컨테이너에 접속 select, insert 수행 (0) | 2023.07.28 |