Search code examples
associationsmybatisspring-mybatis

MyBatis association without column


In a mapper A I have got a resultMap which contains an association loaded using another mapper's (mapper B) method and the method on the mapper B has no arguments.

How should the association look like in the mapper A?

I've tried the following:

<association property="property" column=""
             select="mapperB.findObjectWithNoArguments"
             javaType="Object"/>

but that does not work, the loaded value is null (even though it actually exists and should be loaded).

Removing the column attribute yields Error parsing Mapper XML. Cause: java.lang.IllegalStateException: Mapping is missing column attribute for property property error.


Solution

  • As it turns out, if you really want to have the mapping done through XML, the only possible approach is to add a fake a column with a truth-y value, e.g. one:

    SELECT
      1 AS column_to_force_association
    FROM ...
    

    and then use this faked column in the association:

    <association property="property" column="column_to_force_association"
                 select="mapperB.findObjectWithNoArguments"
                 javaType="Object"/>
    

    But this looks nowhere to optimal, so the better approach is to probably load the entity without the value and set it in a service using a setter.