Можно ли использовать @SqlResultSetMapping для сопоставления сложного объекта Dto - PullRequest
0 голосов
/ 16 января 2020

В настоящее время у меня установлен именованный собственный запрос в CrudRepository, где я объединяю несколько таблиц, и мне нужно отобразить этот результат запроса в Dto.

select
event_id, replaced_by_match_id, scheduled, start_time_tbd, status, away_team_competitor_id, home_team_competitor_id, round_round_id, season_season_id, tournament_tournament_id, venue_venue_id,
competitorHome.competitor_id as home_competitor_competitor_id, competitorHome.abbreviation as home_competitor_competitor_abbreviation, competitorHome.country_code as home_competitor_ccountry_code, competitorHome.ioc_code as home_competitor_ioc_code, competitorHome.rotation_number as home_competitor_rotation_number, competitorHome.virtual as home_competitor_virtual,
competitorAway.competitor_id as away_competitor_competitor_id, competitorAway.abbreviation as away_competitor_competitor_abbreviation, competitorAway.country_code as away_competitor_ccountry_code, competitorAway.ioc_code as away_competitor_ioc_code, competitorAway.rotation_number as away_competitor_rotation_number, competitorAway.virtual as away_competitor_virtual,
homeTeamTranslation.competitor_competitor_id as home_team_translation_competitor_competitor_id, homeTeamTranslation.language_language_id as home_team_translation_language_language_id, homeTeamTranslation.competitor_name as home_team_translation_competitor_name, homeTeamTranslation.competitor_country as home_team_competitor_country,
awayTeamTranslation.competitor_competitor_id as away_team_translation_competitor_competitor_id, awayTeamTranslation.language_language_id as away_team_translation_language_language_id, awayTeamTranslation.competitor_name as away_team_translation_competitor_name, awayTeamTranslation.competitor_country as away_team_competitor_country
from "event" as e
left join competitor as competitorAway on competitorAway.competitor_id = e.away_team_competitor_id
left join competitor as competitorHome  on competitorHome.competitor_id  = e.home_team_competitor_id
left join competitor_translation as homeTeamTranslation on competitorHome.competitor_id = homeTeamTranslation.competitor_competitor_id
left join competitor_translation as awayTeamTranslation on competitorAway.competitor_id = awayTeamTranslation.competitor_competitor_id
where awayTeamTranslation.language_language_id  = 'en' and homeTeamTranslation.language_language_id = 'en'

Я пытаюсь использовать @SqlResultSetMapping аннотация для отображения результата в классы Dto, но безуспешно.

Я настроил отображение таким образом

@SqlResultSetMapping(
    name = "mapLocalizedEvent",
    classes = [ConstructorResult(
            targetClass = TranslatedLocalEvent::class,
            columns = arrayOf(
                    ColumnResult(name = "event_id"),
                    ColumnResult(name = "scheduled"),
                    ColumnResult(name = "start_time_tbd"),
                    ColumnResult(name = "status"),
                    ColumnResult(name = "replaced_by_match_id")
            )
    )]
)

, и он работает нормально, когда все используемые ColumnResult являются простыми типами String или Boolean. Он сопоставляется с объектом TranslatedLocalEvent, похожим на этот

class TranslatedLocalEvent(
    val eventId: String? = null,
    val scheduled: String? = null,
    val startTimeTbd: Boolean? = null,
    val status: String? = null,
    val replacedByMatchId: String? = null
)

Есть ли способ, которым я могу использовать этот подход для сопоставления сложного объекта? Объект TranslatedLocalEvent должен содержать объект TranslatedLocalCompetitor, созданный из частей столбцов, возвращаемых запросом

class TranslatedLocalEvent(
    val eventId: String? = null,
    val scheduled: String? = null,
    val startTimeTbd: Boolean? = null,
    val status: String? = null,
    val replacedByMatchId: String? = null,
    val homeTeam: TranslatedLocalCompetitor? = null
)


public class TranslatedLocalCompetitor(
    val competitorId: String? = null
    val competitorName: String? = null
    val competitorCountry: String? = null
)

1 Ответ

0 голосов
/ 16 января 2020

Самый простой способ, который я вижу, в вашем конструкторе TranslatedLocalEvent принять все столбцы, а в contstructor создать и назначить объект TranslatedLocalCompetitor.

...