Framework/Springboot

[Springboot] JPA Projection ν”„λ‘œμ μ…˜ ν•˜λŠ” 방법

yuri lee 2023. 2. 11. 19:31
λ°˜μ‘ν˜•

Intro

μ•ˆλ…•ν•˜μ„Έμš”. 이번 μ‹œκ°„μ—λŠ” Springboot μŠ€ν”„λ§λΆ€νŠΈ JPAμ—μ„œ ν”„λ‘œμ μ…˜ ν•˜λŠ” 방법에 λŒ€ν•΄ μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€. 

Projection?

λ¨Όμ € 듀어가기에 μ•žμ„œ κ°„λ‹¨ν•˜κ²Œ ν”„λ‘œμ μ…˜μ— λŒ€ν•œ 의미λ₯Ό μ‚΄νŽ΄λ³Όκ²Œμš”. ν”„λ‘œμ μ…˜μ€ μ—”ν‹°ν‹°μ˜ 속성이 λ§Žμ„ λ•Œ 일뢀 λ°μ΄ν„°λ§Œ κ°€μ Έμ˜€λŠ” 방법을 μ˜λ―Έν•©λ‹ˆλ‹€. λ” μžμ„Έν•œ λ‚΄μš©μ΄ κΆκΈˆν•˜μ‹œλ©΄ 일전에 μ œκ°€ 정리해놓은 ν¬μŠ€νŒ… μ°Έκ³  λ°”λžλ‹ˆλ‹€ :)

 

 [MongoDB] ν”„λ‘œμ μ…˜ (Projection) μ΄λž€? projection query μ‚¬μš©λ²•

 

How to do

μ•„λž˜μ˜ User domain 객체λ₯Ό μ°Έκ³ ν•˜μ—¬ μ„€λͺ…ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€. 

@Document(collection = "user")
class User(
    val userId: String?,
    val userName: String?,
    val userPassWord: String?,
    val email: String?,
)

 

μ›ν•˜λŠ” 데이터 속성을 μ•„λž˜μ™€ 같이 Query μ–΄λ…Έν…Œμ΄μ…˜μ„ 톡해 repositoryμ—μ„œ μ‘°νšŒν•΄μ˜€λ©΄ λ©λ‹ˆλ‹€. 

 @Query(fields = "{'userId':  1, 'userPass':  1, 'userName':  1}")

 

User κ°μ²΄μ—μ„œ userId와 userName을 κ°€μ Έμ˜€κΈ° μœ„ν•΄ UserRepositoryμ—μ„œ @Query μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ μž‘μ„±ν•΄λ³Έ μ˜ˆμž…λ‹ˆλ‹€. 

interface UserRepository : BaseReactiveRepository<User, ObjectId> {
    @Query(fields = "{'userId': 1, 'userName':  1}")
    fun findByUserIdAndUserName(userId: String, userName: String): Mono<Read?>
}

 

λ°˜μ‘ν˜•