inflearn logo
강의

Khóa học

Chia sẻ kiến thức

JOOQ ngoài đời thực! Gõ SQL an toàn với Java

Tạo điều kiện tìm kiếm đơn giản bằng Điều kiện

dslContext 작성 시 fetchGroups 이렇게 작성해도 될까요?

Đã giải quyết

639

chhong

4 câu hỏi đã được viết

2

fun findActorFilmography(searchOption: ActorFilmographySearchOption): List<ActorFilmography> {
    val actorListMap = dslContext.select(
            ACTOR,
            FILM,
    ).from(ACTOR)
            .join(FILM_ACTOR).on(ACTOR.ACTOR_ID.eq(FILM_ACTOR.ACTOR_ID))
            .join(FILM).on(FILM.FILM_ID.eq(FILM_ACTOR.FILM_ID))
            .where(
                    containsIfNotBlank(ACTOR.FIRST_NAME.concat(" ").concat(ACTOR.LAST_NAME), searchOption.actorName),
                    containsIfNotBlank(FILM.TITLE, searchOption.filmTitle),
            )
            // 이 부분!!
            .fetchGroups(
                    { record -> record[ACTOR.name, Actor::class.java] },
                    { record -> record[FILM.name, Film::class.java] }
            )

    return actorListMap.entries
            .map { entry -> ActorFilmography(entry.key, entry.value) }
}

private fun containsIfNotBlank(field: Field<String?>, inputValue: String?): Condition {
    if (inputValue.isNullOrBlank()) {
        return DSL.noCondition()
    }

    return field.likeRegex(inputValue)
}

alias을 사용하지 않고, name으로 추출해봤더니 데이터가 정상적으로 잘 나와서요!!

 

혹시나 예외 케이스도 있을 수 있을 것 같아 질문합니다!

java sql spring-boot jooq dsl

Câu trả lời 5

1

sdm32851630

답변은 이정도로 충분할것같은데요!

해당 질문은 완료처리하도록 하겠습니다 ㅎㅎ

추가로 궁금한 점이 있으시다면 댓글 달아주시면 답변해드리도록 하겠습니다

1

sdm32851630

chhong 님 안녕하세요!

 

결론부터 말하면 가능합니다.

그리고 지금 생각해보니 chhong 님이 말씀해주신 방식이 훨씬 좋아보이네요 ㅎㅎ

이 질문은 강의노트에 추가해놓도록 하겠습니다 🙂


이 방식은 실제 문서에도 기재되어있는 방식입니다.. (링크)

 

강의를 촬영하면서 명시적으로 alias를 붙이는 부분도 넣으려다보니 이부분에 대한 설명이 빠졌었었네요.

이 방식을 사용하면 alias에 문자열을 추가하지 않아도 원하는 결과를 얻어 낼 수 있습니다.

 

alias 설정없이 JActor.ACTOR 로 쿼리를 실행하면 내부 alias가 'actor'로 실행되는데요.

이 alias를 name으로 가져 올 수 있습니다. (자바의 경우 $name() 메서드)

 

image

(이는 자동생성되는 JActor 클래스 내부 생성자에서 알 수 있습니다.)

image

image

0

chhong

답변 감사합니다! ㅎㅎㅎ

-4

Nina Williams

My name is Nina Williams and I am divorced. I had three kids with my ex-husband who messed up my credit report before we got divorced, he messed up my credit report. I was really frustrated as life with my kids became a living hell as a single mother.. So I met a friend in the church who told me about H A C K M A V E N S CREDIT SPECIALIST. I contacted them and in less than 7 days they got my credit fixed and increased my score from 515 to 785. I now can take loans, own a house and a business. You can reach out to them via EMAIL: H A C K M A V E N S 5 @ G M A I L. C O M or Call/Text/WhatsApp: [+ 1 (2 0 9) 4 1 7 – 1 9 5 7] if you have any credit related.

-4

Nina Williams

My name is Nina Williams, and I am divorced. I have three kids with my ex-husband, who messed up my credit report before we got divorced. This made my life with my kids a living hell, and I felt frustrated. However, I met a friend in church who recommended H A C K M A V E N S CREDIT SPECIALIST. I contacted them, and within 7 days, they fixed my credit and increased my score from 515 to 785. Now, I can take loans, own a house, and run a business. If you have any credit-related issues, you can reach out to them via EMAIL: H A C K M A V E N S 5 @ G M A I L. C O M or Call/Text/Whats-App: [+ 1 (2 0 9) 4 1 7 – 1 9 5 7].

안녕하세요 FetchGroups에서 Multiset방식 사용하면 어떤가 해서 물어봅니다

0

60

1

monosoul jooq plugin-docker registry 변경

1

101

3

질문) jOOQ 프로젝트에서 CI/CD 자동화 문의

1

176

3

generate dao 를 통한 삽입 시 pk auto-increment 가 적용되지 않습니다

1

109

1

jooq+flyway에서 질문드립니다

0

250

2

안녕하세요 생성일,생성자,수정일,수정자는 자동으로 해주는 기능이 있을까요?

0

209

2

[사례] Maven에서 TestContainers 플러그인 적용시 예외발생

0

143

0

Routines 관련 질문

1

159

2

JOOQ 환경설정 중에 db 패스워드 관련 질문입니다.

1

173

1

Sakila DB 도커 컴포즈 구성 시 팁

1

152

1

DB 벤더별 내장함수에 대한 방언 지원 듣고 질문드려요

1

176

1

인텔리제이 테마 질문

2

426

3

kotlin jOOQ , 컴파일시 힙메모리 부족

0

526

2

pojo 에 setter 가 없는 경우가 있을까요?

1

189

1

FilmWithActor로 fetch 시 select와 생성자의 매개변수 순서가 다를 경우 map이 정상적으로 이루어 지지않는 케이스

1

228

1

from절 subquery table filed nullable 처리

1

221

1

kotlin mapping error

0

517

4

db column은 not null로 되어 있는데 kotlin에서 pojo가 다 nullable로 생성되요.

0

515

1

하나의 디비를 여러 백엔드 서버가 필요로 할 때, flyway 관리

1

889

5

update with dto 질문

1

268

1

R2DBC 환경에서 jOOQ generate 된 Dao를 사용할 수 있는 방법이 있을까요?

2

1246

3

IDE로 스프링부트 실행없이 jooq로 짠 쿼리 결과를 바로 확인하는 방법이 있을까요?

1

503

1

build.gradle.kts

3

758

2

마리아 DB 쓰시는분들은 mysql 파일 쓰시면됩니다.

1

371

1