I am usiing Kotlin. + MyBatis with MysSql database. I have next classess in Kotlin:
class DBBoosterTemplate(
val uuid: String,
val enabled: Boolean,
val image: String?,
val name: String,
val description: String,
val useCount: Int,
val effect: String,
val effectPower: Float,
val price: Int,
) {
var localization: DbLocalization? = null
}
@Serializable
class DbLocalization(
val uuid: String,
val entityId: String,
val data: List<DbKeyValue>,
)
@Serializable
class DbKeyValue(
val locale: String,
val key: String,
val value: String
)
and i have next simple mapper withleft join.
Select templates with localization. ONE to ONE
And here how it looks like:
<mapper namespace="com.simple.games.data.dao.BoosterTemplateDao">
<resultMap id="LocalizationResult" type="com.simple.games.data.model.DbLocalization">
<result property="uuid" column="l_uuid"/>
<result property="entityId" column="l_entity_id"/>
<collection property="data" column="l_data"
typeHandler="com.simple.games.data.handler.LocalizationDataListHandler"/>
</resultMap>
<resultMap id="DBBoosterTemplateResult" type="com.simple.games.data.model.DBBoosterTemplate">
<result property="uuid" column="b_uuid"/>
<result property="enabled" column="b_enabled"/>
<result property="image" column="b_image"/>
<result property="name" column="b_name"/>
<result property="description" column="b_description"/>
<result property="useCount" column="b_use_count"/>
<result property="effect" column="b_effect"/>
<result property="effectPower" column="b_effect_power"/>
<result property="price" column="b_price"/>
<association property="localization" resultMap="LocalizationResult"/>
</resultMap>
<!-- Single-query JOIN select which returns boosters and their localizations in one go -->
<select id="getAll" resultMap="DBBoosterTemplateResult">
SELECT b.uuid AS b_uuid,
b.enabled AS b_enabled,
b.image AS b_image,
b.name AS b_name,
b.description AS b_description,
b.use_count AS b_use_count,
b.effect AS b_effect,
b.effect_power AS b_effect_power,
b.price AS b_price,
l.uuid AS l_uuid,
l.entity_id AS l_entity_id,
l.data AS l_data
FROM boosters b
LEFT JOIN localization l ON b.uuid = l.entity_id
</select>
</mapper>
But unfortunally it seems like MyBatis tries to pas all select colmns to localization. So this mapper works for Booster Template. But it it does not work for Localization. it fails with next error:
Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in com.simple.games.data.model.DbLocalization matching [java.lang.String, java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, java.lang.String, java.lang.Float, java.lang.Integer, java.lang.String, java.lang.String, java.lang.String]\n### The error may exist in file
I have spend couple of hours wit different AI - no luck. So I am asking community to help with this simple left JOIN!
<constructor>,<idArg>and<arg>. It is also important to specify the right<id>(or<idArg>for constructor mapping) when the result map contains<association>or<collection>. Note, however, that MyBatis 3.5.x does not fully support a constructor takingCollectionarguments (see #101). Soon-to-be-released 3.6 will (it would be great if you could test 3.6.0-SNAPSHOT).@Serializableaffects class definition). In case you need further assistance, create a small demo project (like these) and share it on your GitHub repo, then I'll take a look when I have some spare time.