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
- Spring Spring boot 차이
- json gson 차이
- REST란
- json
- 스프링 부트 기능
- Q 클래스
- jwt메서드
- uncheck Exception
- ERD 작성
- JPA주의사항
- jpa에러
- github
- 스프링부트오류
- 1차캐시
- 복합키
- 빈생성안됨
- jpa회원가입
- Filter
- JoinColumn
- git
- 인텔리제이
- JPA
- spring서버
- 스프링 부트 공식 문서
- Error creating bean with name
- Unsatisfied dependency
- queryDSL
- 최종 프로젝트
- @IdClass
- REST API 규칙
Archives
- Today
- Total
Everyday Dev System
JPA) N : 1 관계 맵핑 (Thread : Channel : User) 본문
package me.chaeyoung.jpa.userChannel;
import me.chaeyoung.jpa.channel.Channel;
import me.chaeyoung.jpa.channel.ChannelRepository;
import me.chaeyoung.jpa.user.User;
import me.chaeyoung.jpa.user.UserRepository;
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 static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@Rollback(value=false)
@Transactional
class UserChannelRepositoryTest {
@Autowired
private UserChannelRepository userChannelRepository;
@Autowired
private ChannelRepository channelRepository;
@Autowired
private UserRepository userRepository;
@Test
void userJoinChannelTest() {
// given
var newChannel = Channel.builder().name("new-group").build();
var newUser = User.builder().username("new User").password("new pass").build();
var newUserChannel = newChannel.joinUser(newUser);
// when
var savedChannel = channelRepository.insertChannel(newChannel);
var savedUser = userRepository.insertUser(newUser);
var savedUserChannel = userChannelRepository.insertUserChannel(newUserChannel);
// then
var foundChannel = channelRepository.selectChannel(savedChannel.getId());
assert foundChannel.getUserChannels().stream()
.map(UserChannel::getChannel)
.map(Channel::getName)
.anyMatch(name -> name.equals(newChannel.getName()));
}
}
assert 코드 부분에 Break Point를 걸어두고 디버깅을 시도해보면
아래와 같이 연관관계가 잘 매핑되어 조회해오는 것을 알 수 있습니다.
'내배캠 주요 학습 > JPA 심화' 카테고리의 다른 글
JPA 복합키 설정하기 - @EmbeddedId (0) | 2023.07.31 |
---|---|
JPA 복합키 설정하기 - @IdClass (0) | 2023.07.31 |
JPA) N : 1 관계 맵핑 (thread : Channel) (0) | 2023.07.31 |
Spring Framework와 JPA 활용 (0) | 2023.07.31 |
Entity Class에 활용 어노테이션 - 1주차 4강 (0) | 2023.07.30 |