강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của chhong9965
chhong9965

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

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

Viết

·

604

·

Đã chỉnh sửa

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으로 추출해봤더니 데이터가 정상적으로 잘 나와서요!!

 

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

javasqlspring-bootjooqdsl

Câu trả lời 5

1

sdm32851630님의 프로필 이미지
sdm32851630
Người chia sẻ kiến thức

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

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

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

1

sdm32851630님의 프로필 이미지
sdm32851630
Người chia sẻ kiến thức

chhong 님 안녕하세요!

 

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

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

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


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

 

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

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

 

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

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

 

image

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

image

image

0

chhong님의 프로필 이미지
chhong
Người đặt câu hỏi

답변 감사합니다! ㅎㅎㅎ

-4

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

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].

Hình ảnh hồ sơ của chhong9965
chhong9965

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

Đặt câu hỏi