我目前在 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
)