Search code examples
javasqlibatis

iBatis Silent Failure to Map Columns


When I am mapping a POJO with iBatis such as below:

Person {
    String firstName
    String lastName;
}

To the query:

select firstName, last_name from Person

I am finding that Person.firstName is populated correctly whereas Person.lastName is null - this happens silently. I can introduce a ResultMap to fix the null issue (by translating last_name -> lastName), but I would like iBatis to throw an error when attempting to map rather than doing the best it can silently...

Does anyone know how I can do this?

Thanks!


Solution

  • I see several issues.

    I'm Blind!

    The class Person { String firstName String lastName; } provides exactly 0 visible elements to iBatis because iBatis neither extends Person nor shares the package of Person. Consider the following:

    public class Person
    {
        private String firstName;
        private String lastName;
    
        public String getFirstName()
        {
            return firstName;
        }
    
        public String getLastName()
        {
            return lastName;
        }
    
        public void setFirstName(final String newValue)
        {
            firstName = newValue;
        }
    
        public void setLastName(final String newValue)
        {
            lastName = newValue;
        }
    }

    Seperate and Not Equal

    last_name is in no way the same as lastName.
    Consider using a result map like the following:

    <resultMap id="theIdOfThisMap"
               class="Person">
        <result property="firstName" column="firstName"/>
        <result property="lastName" column="last_name"/>
    </resultMap>
    
    <select id="searchForEntities"
            resultMap="theIdOfThisMap">
        select
            firstName,
            last_name
        from
            PERSON
    </select>