πŸ’₯λ„μ΄μ–΄μ„œλŠ” μ•ˆλ©λ‹ˆλ‹€.

졜재혁·2022λ…„ 8μ›” 1일
1
post-thumbnail

Builder λ₯Ό ν™œμš©ν•˜μ—¬ ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό μž‘μ„±ν–ˆλŠ”λ°, κ·Έ 쀑 λ°œμƒν•œ μ—λŸ¬


Builder νŒ¨ν„΄μ— λŒ€ν•΄ κ³΅λΆ€ν•΄λ³΄λ‹ˆ Builder νŒ¨ν„΄μ΄ μƒμ„±μž νŒ¨ν„΄μ— λΉ„ν•΄ 쒋은 점 쀑 ν•˜λ‚˜κ°€,

객체 생성 μ‹œμ—, λ™μ μœΌλ‘œ ν•„μš”ν•œ ν•„λ“œλ“€λ§Œ ν• λ‹Ήν•΄ 쀄 수 μžˆλ‹€λŠ” μ μ΄μ—ˆλ‹€



λ‚˜λŠ” 쿼리 λ©”μ†Œλ“œλ‘œ μƒμ„±ν•œ λ‹¨μˆœνžˆ username 으둜 νšŒμ›μ„ κ²€μƒ‰ν•˜λŠ” κΈ°λŠ₯을 ν…ŒμŠ€νŠΈν•˜κ³  μ‹Άμ—ˆλ‹€.

 @Query("select m from Member m where m.username= :username")
 Member findByUsername(@Param("username") String username);

그리고, λ‹¨μˆœνžˆ member 에 λΉŒλ”λ‘œ username 만 ν• λ‹Ήν•΄μ„œ ν…ŒμŠ€νŠΈλ₯Ό μ§„ν–‰ν–ˆλ‹€.

	@Test
    public void μ΄λ¦„μœΌλ‘œμ°ΎκΈ°() {
        // given
        Member member = Member.builder()
                	.username("AAA")
                	.build();
        memberRepository.save(member);

😲그런데 β€˜λ„μ΄μ–΄μ„œλŠ” μ•ˆλ©λ‹ˆλ‹€β€™ λΌλŠ” μ—λŸ¬ 메세지가 μ—„μ²­λ‚˜κ²Œ μ°ν˜”λ‹€β€¦..

λ‹€ν–‰νžˆ λ”± 보자마자 문제λ₯Ό μ•Œμ•˜λŠ”λ°,

Memeber 엔티티에 @NotNull 둜 μ œμ•½μ‘°κ±΄μ„ κ±Έμ–΄μ€€ ν•„λ“œλ“€μ„ λ‚΄κ°€ 할당을 μ•ˆν–ˆκΈ° λ•Œλ¬Έμ΄μ—ˆλ‹€.

λ‹€μ‹œ λΉŒλ”μ— @NotNull 둜 μ„€μ •λœ ν•„λ“œλ“€κΉŒμ§€ λ„£μ–΄μ„œ ν…ŒμŠ€νŠΈμ½”λ“œλ₯Ό λŒλ €λ³΄λ‹ˆ μ œλŒ€λ‘œ ν…ŒμŠ€νŠΈκ°€ μ„±κ³΅ν•˜μ˜€λ‹€.


🀨 λŠλ‚€μ 

λ‹€ 쓰지도 μ•Šμ„ ν•„λ“œλ₯Ό @NotNull μ œμ•½μ‘°κ±΄λ•Œλ¬Έμ— 맀번 μ μ–΄μ€˜μ•Ό ν•˜λ‚˜..?

λ‹€λ₯Έ 방법이 μžˆμ„ν…λ° ν˜„μž¬λ‘œμ„œλŠ” λͺ¨λ₯΄κ² μŒ

profile
잘λͺ»λœ 고민은 μ—†μŠ΅λ‹ˆλ‹€

1개의 λŒ“κΈ€

comment-user-thumbnail
2022λ…„ 9μ›” 15일

λ„ˆλ¬΄ μœ μš©ν•΄μš©!

λ‹΅κΈ€ 달기