0

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!

3
  • 1
    For constructor mapping, you need to use <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 taking Collection arguments (see #101). Soon-to-be-released 3.6 will (it would be great if you could test 3.6.0-SNAPSHOT). Commented Sep 12 at 12:57
  • Weird, And it works for my Localization case. I only did not use it only becouse somehow i cannot use <constructor> for DBBoosterTemplate. But you helped me a lot. Thanks Commented Sep 12 at 13:36
  • I am not familiar with Kotlin, so I could well be missing something (e.g. I'm not sure how that @Serializable affects 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. Commented Sep 12 at 16:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.