Manuelarte
Manuelarte

Reputation: 1810

ResultMap mybatis not working

I have this simple mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0 Powerhouse//EN" "http://mybatis.org/dtd/mybatis-3-Powerhouse-mapper.dtd">

<mapper namespace="nl.powerhouse.data.domain.CountryMapper">
<resultMap id="countryDsoMap" type="nl.powerhouse.domain.common.types.algemeen.Land">
    <id column="land_id"/>
    <association property="landId" columnPrefix="land_" column="id"/>
    <association property="landcode" columnPrefix="land_" column="landcode"/>
    <association property="landnaam" columnPrefix="land_" column="landnaam"/>
    <association property="landnummer" columnPrefix="land_" column="landnummer"/>
    <association property="handelsland" columnPrefix="land_" column="handelsland"/>
</resultMap>

<select id="findCountryByCode" resultMap="countryDsoMap">
    select <includeColumns tableAlias="land" columnPrefix="land_" refid="nl.powerhouse.data.dao.algemeen.LandMapper.Base_Column_List"/>
    from alg_t_land land
    where landcode = #{countryCode}
</select>

And this Land.java class

public final class Land implements Serializable {

    private LandId landId;
    private String landcode;
    private String landnaam;
    private String landnummer;
    private boolean handelsland;

    public Land() {
        // default constructor for mybatis
    }
    // getters...
}

But, when I am trying to use it, I get the following error:

org.mybatis.spring.MyBatisSystemException: nested exception is    org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NullPointerException
### The error may exist in nl/powerhouse/data/domain/CountryMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
...
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)

I know that the query is working, the problem is with the resultMap, any hint?

Upvotes: 1

Views: 3223

Answers (1)

Pau
Pau

Reputation: 16086

The problem is that you are using <association> tag instead of <result> which is the tag used to JavaBean properties as you can see in the documentation of MyBatis:

ResultMap

result – a normal result injected into a field or JavaBean property

association – a complex type association; many results will roll up into this

Definitively, your mappings are not complex type association so instead of use <association> you must use <result>.

Your result map would be:

<resultMap id="countryDsoMap" type="nl.powerhouse.domain.common.types.algemeen.Land">
    <id column="land_id"/>
    <result property="landId" columnPrefix="land_" column="id"/>
    <result property="landcode" columnPrefix="land_" column="landcode"/>
    <result property="landnaam" columnPrefix="land_" column="landnaam"/>
    <result property="landnummer" columnPrefix="land_" column="landnummer"/>
    <result property="handelsland" columnPrefix="land_" column="handelsland"/>
</resultMap>

Upvotes: 1

Related Questions