spring-bootkotlinspring-data-jpasqlresultsetmapping

Can @SqlResultSetMapping be used to map a complex Dto object


I currently have a named native query set up in CrudRepository where I'm joinnig few tables and I need to map that query result into a 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'

I'm trying to use @SqlResultSetMapping annotation to map result into Dto classes but unsuccessfully.

I've set up mapping this way

@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")
            )
    )]
)

and it is working fine where all of the ColumnResult used are simple types String or Boolean. It maps to object TranslatedLocalEvent looking like this

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

Is there a way I can use this approach to map a complex object? TranslatedLocalEvent object needs to contain TranslatedLocalCompetitor object built from parts of columns query returnes

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
)

Solution

  • The easiest way i see is in your TranslatedLocalEvent constructor accept all columns and in the contstructor create and assign the TranslatedLocalCompetitor object.